I have created a Django Form but it does not submit after hitting the submit Button. It seems like the HTML is broke because if i hit the submit button Django doesn´t output a POST Action. Template is as follows:
<form method="POST">
{% csrf_token %}
<div >
<div >
<div >
<div >
{{ addForm.email }}
{{ addForm.givingLessons }}
</div>
</div>
</div>
</div>
<input type="submit" value="Update" />
</form>
views.py
def manage_students(request):
if request.method == "POST":
form = addStudentGivingLessons(request.POST)
if form.is_valid():
lessons = form.cleaned_data.get('givingLessons')
print(lessons)
return redirect('home')
else:
print("form is not Valid")
else:
#rendering View
Forms.py
class addStudentGivingLessons(forms.Form):
email = forms.EmailField()
givingLessons = forms.ModelMultipleChoiceField(widget = forms.CheckboxSelectMultiple,
queryset = Faecher.objects.all())
CodePudding user response:
How are you generating your HTML form markup? Have you got method post e.g. <html method="post">
?
EDIT
Just seen your edit as you've added markup now. I can see that you have got method as post. So, ignore the above please.
You can try to print the errors to check if it shows any validation errors
print(form.error)
Also, can you try to print the post data and see if it has got any data?
def manage_students(request):
print(list(request.POST.items()))
CodePudding user response:
I just found my mistake. I had a Form in my HTML before where i didn´t close the Tag. Now it´s working ...