Specific questions related to forms with the Django web framework
Specific questions related to Django forms.
Quote from the official documentation:
While it is possible to process form submissions just using Django’s
HttpRequest
class, using the form library takes care of a number of common form-related tasks. Using it, you can:
- Display an HTML form with automatically generated form widgets.
- Check submitted data against a set of validation rules.
- Redisplay a form in the case of validation errors.
- Convert submitted form data to the relevant Python data types.
An example form from the docs
from django import forms
class ContactForm(forms.Form):
subject = forms.CharField(max_length=100)
message = forms.CharField()
sender = forms.EmailField()
cc_myself = forms.BooleanField(required=False)
An example on how to validate a form with POST data
form = ContactForm(request.POST) # A form bound to the POST data
if form.is_valid():
return HttpResponseRedirect('/thanks/')