Reputation: 231
I am trying to use ModelForms and CBVs to handle them, but I am facing trouble especially while submitting my form. Here's my code.
forms.py
from django import forms
from .models import Volunteer
class NewVolunteerForm(forms.ModelForm):
class Meta:
model = Volunteer
fields = '__all__'
views.py
from django.http.response import HttpResponse
from django.views.generic.edit import CreateView
from .forms import NewVolunteerForm
class NewVolunteerView(CreateView):
template_name = 'website/join.html'
form_class = NewVolunteerForm
def form_valid(self, form):
print('Submitting')
form.save()
return HttpResponse('DONE')
join.html
{% extends 'website/_base.html' %}
{% block title %}Join Us{% endblock title %}
{% block content %}
<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<input type="submit">
</form>
{% endblock content %}
The form is getting displayed correctly with no issues at all, but when I fill it in and press the submit button it simply re-rendered the form and doesn't submit it at all.
Upvotes: 1
Views: 22
Reputation: 231
I solved this by adding the enctype="multipart/form-data"
attribute to my <form>
element.
The reason was when you have ImageFields or FileFields this attribute should be used.
Upvotes: 1