python3.4 + Django1.7.7 Some problems with forms

Source: Internet
Author: User

The above is not called cleaned_data the results of the submission, the visible template directly to the form of the entire label is received.




The following is the result of calling Cleaned_data





The Django form, which is then submitted, is this:

#coding: Gb2312from Django Import Formsclass contactform (forms. Form):    subject = forms. Charfield (max_length=10,label= ' subject ') #设置最大长度为10    email = forms. Emailfield (required=false,label= ' Email ') #非必要字段    message = forms. Charfield (widget=forms. textarea,label= ' message ') #指定form中组件的类型    #自定义校验规则, which is automatically called by the system at checksum, in order after "field constraint"    def clean_message (self):        message = self.cleaned_data[' message '] #能到此处说明数据符合 field constraint requires        num_words = Len (Message.split ())        if num_ Words < 1: #单词个数            raise forms. ValidationError ("Your word is too short!")        Return message


For example, the following sentence:


email = forms. Emailfield (required=false,label= ' Email ') #非必要字段

Can actually be used as a non-essential field, Required=false


Since the call to form.cleaned_data# only has the corresponding cleaned_data when each field meets the requirements, it must have been before:

If Form.is_valid (): #说明各个字段的输入值都符合要求

Therefore, the above fields Required=false, in testing things or writing their own things, such as the safety of the occasion is more necessary


#coding: Gb2312from django.http import httpresponseimport datetime,calendarimport timefrom django.http Import Httpresponsefrom django.template Import contextfrom django.template.loader import get_templatefrom django.http Import HttpResponse, http404from django.contrib.auth.models import userfrom django.shortcuts import Render_to_responsefrom Django.http Import httpresponseredirectfrom django.contrib.auth import logoutfrom django.template Import Requestcontextfrom django.core.urlresolvers Import reversefrom django.shortcuts import redirect#from Django Import Formfrom django.shortcuts import render from. Forms Import Contactform #from django.shortcuts import render_to_response#f Rom django_manage_app.forms import contactformdef current_datetime (Request): now = Time.strftime ('%y-%m-%d-%h-%m-%s ', Time.localtime (Time.time ())) HTML = ' 



HTML submitted to the template:






python3.4 + Django1.7.7 Some problems with forms

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.