Django Form Verification implementation in simple form verification implementation
I. Form implementation using forms
1: We generally setFormClasses are put in a file:Forms. py. Specifically, you can store 'in the APP directory, for example, my APP is in the BOOKS directory.
Add the following in forms. py:
from django import formsclass ContactForm(forms.Form): subject = forms.CharField() email = forms.EmailField(required=False) message = forms.CharField()
Each field is required by default. To enableEmailTo be optional, We need to specifyRequired = False.
2: Call forms in the view and add
From django. shortcuts import render_to_responsefrom mysite. contact. forms import ContactFormdef contact (request): if request. method = 'post': form = ContactForm (request. POST) if form. is_valid (): cd = form. cleaned_data send_mail (cd ['subobject'], cd ['message'], cd. get ('email ', 'noreply @ example.com'), ['siteowner @ example.com '],) return HttpResponseRedirect ('/contact/thanks/') else: form = ContactForm () # Add the last sentence. Otherwise, return render_to_response ('Contact/contact_form.html ', {'form': form}, context_instance = RequestContext (request ))
3: create a new form directory and Form file in templates. The directory location is/templates/contact/contact_form.html.
contact_form.html
4: add the view path to the url to access