I. Overview
Django has a form library called django. forms. Define a form class for each HTML "
For the first time, I felt that Django was really powerful and powerful to abnormal.
1 from django import forms2 3 class ContactForm(forms.Form):4 subject = forms.CharField()5
Win7
Python2.7
Django 1.6.5
Because the database SQLite is used and the project contains models. py, forms. modelform is easy to use to process forms.
The default defastorage storage is used to upload image files.
1. Add two media definitions in settings. py:
#MediaMEDIA_ROOT = 'e:/Depot/media'MEDIA_URL = '/media/'
2. Models. py
# Coding: utf8from
Python Django uses forms to implement comments and django comments
It seems that Django has abandoned the use of its own comments since version 1.6. The specific reason is not found. However, the comment function can be implemented by using the internal modules of Django, th
, equivalent to whereTest.objects.filter (id=1)2. Get a single object, equivalent to whereTest.objects.get (id=1)3, sort, equivalent to the orderTest.objects.order_by ("id") can be chained using Test.objects.filter (name= "Runoob"). Order_by ("id")4. Get all data, equivalent to select * FROMList = Test.objects.all ()Change dataChange data using Save (), or update ()Delete dataCall Delete () toDjango Processing a form
HTML forms are an important
Form IntroductionIn summary, the main functions of the form component are as follows:Generate HTML tags available for a pageVerifying the data submitted by the userKeep Last inputPrompt for error messagesNormal mode handwriting registration function views.pydef register (Request): Name_error = ' if Request.method = = ' POST ': user = Request. Post.get (' user ') pwd = Request. Post.get (' pwd ') if Len (user) Register.htmlUse the form component to implement the registr
I. http://www.php.cn/wiki/1515.html "target=" _blank ">django form validation introduced
Sometimes we need to use get,post,put and other methods in the foreground HTML page to submit some data to the background processing example;
Front-end Submission background acquisition:
From django.shortcuts import render,httpresponse,redirectfrom app01 import modelsdef Login (Request): if Request.method = = "POST": username = Request. Post.get ("username")
For details about how to use forms in the Django framework of Python, djangoforms
Form Functions
Automatically generate HTML form elements
Check the validity of form data
If a verification error occurs, re-display the form (data is not reset)
Data type conversion (convert data of character type to corresponding Python type)
Form-related objects include
Widget: a tool used to render HTML element
1. The form component in Django component-forms component has the following functions:
1) generate HTML tags
2) Verify user data (Display Error Information)
3) HTML form submission retain the data submitted last time
4) content displayed on the initialization pageField Verification
The previously written view function adds the submitted data to the database without verification. This is wrong!
For example,
Forms: Field Verification
For a single instance: Description of the registered user.
Model: models. py
class UserInfo(models.Model): name=models.CharField(max_length=32) pwd=models.CharField(max_length=32) email=models.EmailField() tel=models.CharField(max_length=32)
Template: register.html:
View function: Register
# Forms component from Django.
ua_display_good1 (request ):Try:UA = request. Meta ['HTTP _ user_agent ']Failed t keyerror:UA = 'unknown'Return httpresponse ("your browser is % s" % UA)Of course, a better way is to use the get function for processing.Def ua_display_good2 (request ):UA = request. Meta. Get ('HTTP _ user_agent ', 'unknown ')Return httpresponse ("your browser is % s" % UA)
2) self. Get, self. PostThese two attributes are too familiar. They are similar to the dictionary but are more powerful than the dictionary.
All that was learned was how to display the data, but there was no information about how to respond to user submissions.Forms are an important way for users to interact with the server in the Web.Import Django.formsForm Django Import FormsWhat is the difference between them?The roles that form plays in Django are:* Show Form* Verify user-submitted dataDefinition of form~~~~~~~~~~The definition of a form is
views.pyDefine the good one RegForm class first:From Django Import forms# write a Class RegForm (forms) as required by the Django form component. Form): name = forms. Charfield (label= "username") pwd = forms. Charfield (la
While we have been using the sample form of book search and will improve it perfectly, this is still fairly rudimentary: it contains only one field, Q. For this simple example, we don't need to use a Django form library to handle it. But a more complicated form needs to be dealt with in many ways, so let's take a look at a more complex example: Site Contact form.
This form includes user-submitted feedback and an optional e-mail address. When this for
, equivalent to whereTest.objects.filter (id=1)2. Get a single object, equivalent to whereTest.objects.get (id=1)3, sort, equivalent to the orderTest.objects.order_by ("id") can be chained using Test.objects.filter (name= "Runoob"). Order_by ("id")4. Get all data, equivalent to select * FROMList = Test.objects.all ()Change dataChange data using Save (), or update ()Delete dataCall Delete () toDjango Processing a form
HTML forms are an important
Django write blog 1. One webpage, two forms, submitted separately, djangoform
New to Django. I have been studying Django for a month and started to write a blog.
Python 2.7.11 Django 1.10.2 Bootstrap 3.3.0 IDE: eclipse Pydev
Models
# User class User (models. model): username
Django's forms and models are just as awesome. He has two functions, one is to generate form forms, and also to validate form forms.Here is a general case of the beam that is often used when generating form forms to set default values.1. Set the default selected option when you need to generate select by default.In the django
used commands:Python manage.py validate# Check models for syntax errorsPython manage.py startproject ProjectNamePython manage.py Startapp AppNamePython manage.py syncdb# creates the newly created table into the database, but if the table is modified later, the use of this command is not updated. You should use the following two commands at this time, only if you are using the Django version of =1.7Python manage.py makemigrationsPython manage.py Migra
This article mainly introduces how to compile Contact forms in the Django framework. Django is the most popular among the frameworks with different Python characteristics, for more information, see the example form that we have been using for book search and can improve it perfectly. However, this is quite simple: it only contains one field, q. In this simple exa
First look at the following simple method: Fromdjango.httpimporthttpresponsedefhello (Request ): returnhttpresponse ("Helloworld") We can get a lot of information request from this request. meta is a Python dictionary that contains header information for all HTTP requests, such as the user's IP address and user agent (usually the name and version number of the browser). Note that the complete list of header information depends on the header information sent by the user and the header informati
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.