Yes thanks for the answer. I´ve tried all this but it´s still not working.
Here is my models:
from django.db import models
from django.urls import reverse
from datetime import datetime
class Category(models.Model):
name = models.CharField(max_length=250, help_text='Kategorie', blank=True)
class Meta:
verbose_name_plural = "Categories"
def __str__(self):
return self.name
class Link(models.Model):
alias = models.CharField(max_length=250, help_text='Linkalias', blank=True)
link = models.URLField(("Links zum Thema"), max_length=128, db_index=True, unique=True, blank=True)
def __str__(self):
return self.alias
class File(models.Model):
name = models.CharField(max_length=250, help_text='Dateiname', blank=True)
upload = models.FileField(upload_to ='uploads/', null=True, blank=False)
def __str__(self):
return self.name
class Tag(models.Model):
name = models.CharField(max_length=250, help_text='tagname', blank=True)
def __str__(self):
return self.name
class Thema(models.Model):
publication_date = models.DateField(verbose_name="Date the Post was published.", default=datetime.now)
title = models.CharField(max_length=70, help_text="The title of the Post")
text = models.TextField(max_length=5000,default='text 1')
text2 = models.TextField(max_length=5000,default='text 2')
text3 = models.TextField(max_length=5000,default='text 3')
# Relation Fields
category = models.ForeignKey(Category, on_delete=models.CASCADE)
links = models.ManyToManyField(Link)
files = models.ManyToManyField(File)
tags = models.ManyToManyField(Tag)
def get_absolute_url(self):
return reverse('thema-detail', kwargs={'pk' : self.pk})
def __str__(self):
return self.title
class Bilder(models.Model):
name = models.CharField(max_length=250, help_text='Bildname', blank=True)
beschreibung = models.CharField(max_length=400, help_text='Kurzbschreibung', blank=True)
thema = models.ForeignKey(Thema, on_delete=models.CASCADE, related_name='bilder', null=True)
image = models.ImageField(null=True, blank=True, upload_to='images/')
class Meta:
verbose_name_plural = "Bilder"
def __str__(self):
return self.name
HERE ARE MY VIEWS:
from .models import Thema
from django.views.generic import ListView, DetailView
from .forms import ThemaForm, BilderForm, LinkForm, CategoryForm, TagForm, FileForm
from django.shortcuts import redirect, render
from django.shortcuts import render
class HomePageView(ListView):
model = Thema
context_object_name = 'themas'
template_name = "home/home.html"
class ThemaView(DetailView):
model = Thema
context_object_name = 'thema'
template_name = "home/thema.html"
def get_name(request):
if request.method == 'POST':
bilder = BilderForm(request.POST, prefix='bilder')
thema = ThemaForm(request.POST, prefix='thema')
link = LinkForm(request.POST, prefix='link')
tag = TagForm(request.POST, prefix='tag')
category = CategoryForm(request.POST, prefix='category')
file = FileForm(request.POST, prefix='file')
if bilder.is_valid() and thema.is_valid() and link.is_valid() and tag.is_valid() and category.is_valid() and file.is_valid():
return redirect('home')
else:
bilder = BilderForm(prefix='name')
thema = ThemaForm(prefix='person')
file = FileForm(prefix='person')
link = LinkForm(prefix='person')
tag = TagForm(prefix='person')
category = CategoryForm(prefix='person')
return render(request, 'home/create.html', {'bilder': bilder, 'thema': thema, 'link': link, 'tag': tag, 'category': category, 'file': file})
AND HERE ARE THE FORMS:
from django import forms
from .models import Bilder, Thema, Category, Link, Tag, File
class BilderForm(forms.ModelForm):
class Meta:
model = Bilder
fields = ('name', 'beschreibung', 'image')
class ThemaForm(forms.ModelForm):
class Meta:
model = Thema
fields = ('title', 'text')
class CategoryForm(forms.ModelForm):
class Meta:
model = Category
fields = ('name',)
class LinkForm(forms.ModelForm):
class Meta:
model = Link
fields = ('alias', 'link')
class FileForm(forms.ModelForm):
class Meta:
model = File
fields = ('name', 'upload')
class TagForm(forms.ModelForm):
class Meta:
model = Tag
fields = ('name',)
HERE IS THE TEMPLATE:
<form method="post">
{% csrf_token %}
{{ bilder.as_p }}
{{ thema.as_p }}
{{ link.as_p }}
{{ category.as_p }}
{{ file.as_p }}
{{ tag.as_p }}
<input type="submit" value="Save">
</form>
I think I also need FormSets. The problem is if I create for example a TAG this way. The tag is saved but at the same time the thema model needs to store the relation to this tag in his own model which it probably can´t because I can´t set it in the form because it isn´t in the database by that time…