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:
fromdjangoim
to use, just need to add , it is OK to define the corresponding form class. In the example below we have only one form class. The common practice of the Django community is to put the form class in a forms.py file, and we'd better follow this rule. Create the disk\forms.py file, and write: # mysite\disk\for
Python: custom form control in Django background, pythondjango
In django, we canadmin.py Add ModelAdminIn this way, you can easily add, delete, modify, and query operations in the background. HoweverModel The generated forms are not friendly. We hope to make various types of controls like front-end development, so we have to customize the forms in the background.
Form#!/usr/bin/env Python#coding:utf8 from django.forms import Form,modelformimport models class Coursemodelform (ModelForm ): class Meta: model = models. Course exclude = () def __init__ (Self,*args,**kwargs): super (Coursemodelform,self). __init__ (*args,** Kwargs) for fieldname in Self.base_fields: #循环给所有字段加样式 field = Self.base_fields[fieldname]
into two types: 1. Data with unchanged content. 2. frequently changed data extracted from databases or files. For the implementation method of 1, see django form (1 ).
If the data is extracted from the database, adding, deleting, and modifying the content will happen frequently. If the implementation method of 1 is used, it is unrealistic, in this case, we need a method to implement dynamic select.
Let's l
Browse Catalogs
Form form upload File
Ajax Uploading Files
Forge Ajax Upload Files
Form form upload file htmlNote: You must add the Enctype= "Multipart/form-data property.Viewdef index (Request): return render (Request, "index.html") def upload_file
support the operation through the AJAX request, get the back end of the data, you will use the following serialization method.Method Onefrom django.core import serializersobj = models.UserType.objects.all()data = serializers.serialize(‘json‘, obj)print(data) # 打印结果在下面# [{"model": "app01.usertype", "pk": 1, "fields": {"name": "\u8d85\u7ea7\u7528\u6237"}}]The Queryset object is returned here. So it can't be serialized with JSON. The serialization is i
This article mainly introduces the Django background custom form control in Python, in fact Django has provided us with some available form controls, such as: Multi-box, radio button, etc., interested in opening to understand.
In Django we can add in admin.py ModelAdmin ,
processed and returned to the user, making it more convenient and reasonable.
Supplement:
5. Four form initialization Methods
① Instantiate oneform (initial = {'onefield ': value })
② Initialize the field value oneformfield = forms. CharField (initial = value)
③ Override the _ init _ () method of the Form class: self. fields ['onefield']. initial = value
④ When
Issue: from After using the form submit, the error is entered:
Action: setting.py django.middleware.csrf.CsrfViewMiddleware’, Comment out this sentence in the Django project file
MIDDLEWARE = [ ‘django.middleware.security.SecurityMiddleware‘, ‘django.contrib.sessions.middleware.SessionMiddleware‘, ‘django.middleware.common.CommonMiddleware‘, #‘django.middleware.csrf.CsrfViewMiddleware
equals the dictionary . returnRender (Request,'ajax_demo.html')Ajax_demp.html"en">"UTF-8"> "text"Id="User"/>"Password"Id="pwd"/>"Button"Value="Submit"onclick="SubmitForm ()"/> #triggers the SubmitForm function when the Submit button is clickedfunction SubmitForm () {#send an AJAX request$.ajax ({URL:'/ajax_demo/',#Submit form contents to/ajax_demo functionType'POST',#Submit as Postdata:{'User':$('#user'). Val (),'pwd':$('#pwd'). Val ()},#get user
Django comes with a form library called Django.forms, which can handle the HTML form display and validation that we mentioned in this chapter. Let's take a closer look at the form library and use her to rewrite the contact form app.
Django's Newforms Library
The term django
detection is completed in the form class. After the data is valid, the data is processed logically without being processed and returned to the user, making it more convenient and reasonable.
Supplement:5. Four form initialization Methods① Instantiate oneform (initial = {'onefield ': value })② Initialize the field value oneformfield = forms. CharField (initial = value)③ Override the _ init _ () method of th
Environment: Python 2.7.5 + Django 1.6
With Django, we can define a Form in declarative mode, as shown below:
django = forms.CharField(max_length=100= forms.CharField(max_length=100)
It is quite comfortable to write, but the problem arises. After I initialize this Form, for
run locally. Load the form, leave all fields blank before submitting a blank form, then fill in an incorrect email address and try to submit the form, and then submit the form with the correct data. (depending on the settings of the server, when Send_mail () is called, you
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.