I am trying to create 2 Model Forms in on Step, one of them is Modelformset, does someone did this before, it will be very helpful. I am using Django 2 in my project. Thank you.
class Post(models.Model):
main_image = models.ImageField('main_image', upload_to='main_images/', blank=True, null=True)
def __str__(self):
return str(self.pk)
class PostImages(models.Model):
image = models.ImageField('Foto', upload_to='post_images/', blank=True, null=True)
post = models.ForeignKey(Post,related_name='myposts',on_delete=models.CASCADE, null=True)
def __str__(self):
return str(self.pk)
my Forms.py
Here I am Trying to create two forms, first one is main_image and the other is formsetfield in one step. that works but i can not get the instance of my formset so i can not save it.
class step3Form(forms.ModelForm):
main_image = forms.FileField(widget=forms.FileInput(attrs={'class': 'custom-control'}), required=True, label=_('Hauptbild:'))
formsetfield = modelformset_factory(PostImages, ImageForm, can_delete=True)
class Meta:
model = Post
fields = ('main_image',)
def __init__(self, *args, **kwargs):
super(step3Form, self).__init__(*args, **kwargs)
views.py
what schuld I do here to get the instance of my Formset? in order to save it!
class PostCreateView(LoginRequiredMixin, SessionWizardView):
instance = None
form_list = [PostForm, PostFormSecondStep, step3Form, step4Form]
file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'media'))
def get_template_names(self):
return [TEMPLATES[self.steps.current]]
def get_form_instance(self, step):
if self.instance is None:
self.instance = Post()
return self.instance
def done(self, form_list, form_dict, **kwargs):
form_data_dict = self.get_all_cleaned_data()
result = {}
self.instance.author = self.request.user.profile
form_list[2].save()
self.instance.save()
form_data = [form.cleaned_data for form in form_list]
return redirect('post_app:post_create_page')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
return context
html
<form method="post" id="msform" enctype="multipart/form-data">
{% csrf_token %}
{{ wizard.management_form }}
{% if wizard.form.forms %}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{% if wizard.form.non_field_errors %}
<ul>
{% for error in wizard.form.non_field_errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
{{ wizard.form.main_image.label_tag }}
{{ wizard.form.main_image }}
{{ wizard.form.main_image.errors }}
{{ wizard.form.formsetfield.errors }}
<div id="formset" data-formset-prefix="{{ wizard.form.formsetfield.prefix }}">
{{ wizard.form.formsetfield.management_form }}
<div data-formset-body>
{% for form in wizard.form.formsetfield %}
{{ form.non_field_errors }}
{{ form.errors }}
<div data-formset-form>
{{ form }}
<span class="p-2"><a class="cursor-pointer" data-formset-delete-button><i class="far fa-trash-alt"></i></a></span>
</div>
{% endfor %}
</div>
<input type="button" class="btn btn-outline-dark" value="Hinzufügen" data-formset-add>
</div>
{% endif %}
<button type="submit" id="submit_btn" class="action-button" name="button">Weiter</button>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" class="action-button" type="submit" value="{{ wizard.steps.first }}">Erster Schritt</button>
<button name="wizard_goto_step" id="back_btn" class="action-button" type="submit" value="{{ wizard.steps.prev }}">Zurück</button>
{% endif %}
</form>
try this in function based:
Forms.py
view.py