Django 表單
#創建一個新項目以及一個APP
django-admin.py startproject csvt07
cd csvt07
django-admin.py startapp blog
#修改配置文件
vim blog/settings.py
#數據庫配置
MANAGERS = ADMINS DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'mydb.db', # Or path to database file if using sqlite3. # The following settings are not used with sqlite3: 'USER': '', 'PASSWORD': '', 'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP. 'PORT': '', # Set to empty string for default. } } #增加blogAPP INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog', # Uncomment the next line to enable the admin: # 'django.contrib.admin', # Uncomment the next line to enable admin documentation: # 'django.contrib.admindocs', )
#修改鏈接對應的處理視圖
vim blog/urls.py
from django.conf.urls import patterns, include, url # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover() urlpatterns = patterns('', # Examples: # url(r'^$', 'csvt07.views.home', name='home'), # url(r'^csvt07/', include('csvt07.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: # url(r'^admin/', include(admin.site.urls)), url(r'^blog/register$','blog.views.register'), )
#創建模板目錄和模板文件register.html
mkdir blog/templates && vim blog/templates/register.html
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/OTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-TYpe" content="text/html; charset=UTF-8" /> <title></title> </head> <body> <form method="post"> `form` <input type="submit" value="ok"/> </form> </body>
#創建對應的視圖處理
vim blog/views.py
# Create your views here. from django import forms from django.http import HttpResponse from django.shortcuts import render_to_response class UserForm(forms.Form): name = forms.CharField() def register(req): if req.method == 'POST': form = UserForm(req.POST) if form.is_valid(): print form.cleaned_data return HttpResponse('ok') else: form = UserForm() return render_to_response('register.html',{'form':form})
#啓動開發服務器
python manage.py runserver
#瀏覽器中訪問開發服務器
http://127.0.0.1:8000/blog/register
pic[form_1]
#點擊ok提交表單的時候會提示如下錯誤,CSRF***。
#官方的的說明
csrf_token
This tag is used for CSRF protection, as described in the documentation for Cross Site Request Forgeries.
pic[form_2]
#解決這個辦法就在settings.py中注視掉CSRF的類
vim blog/settings.py
MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', # 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', # Uncomment the next line for simple clickjacking protection: # 'django.middleware.clickjacking.XFrameOptionsMiddleware', )
#再次點擊ok提交返回如下,證明成功提交表單數據。
pic[form_3]
#開發服務器會顯示一個字典的數據,因爲我們這沒有對數據做處理只是簡單的輸出
pic[form_4]
#如果提交的表單數據是空的話會提示如下信息
pic[form_5]