Django Form Verification implementation in simple form verification implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.