在表单提交前进行验证的几种方式整理_javascript技巧
程序员文章站
2022-05-30 15:46:42
...
在表单提交前进行验证的几种方式 .
在Django中,为了减轻后台压力,可以利用JavaScript在表单提交前对表单数据进行验证。下面提供了有效的几种方式(每个.html文件为一种方式)。
formpage1.html
Example1
提交表单前进行验证(方法一)
formpage2.html
Example2
提交表单前进行验证(方法二)
formpage3.html
Example3
提交表单前进行验证(方法三)
以下是视图函数、URL配置以及相关设置
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
views.py
#coding: utf-8
from django.http import HttpResponse
from django.shortcuts import render_to_response
def DealWithForm1(request):
if request.method=="POST":
FirstName=request.POST.get('firstname','')
LastName=request.POST.get('lastname','')
if FirstName and LastName:
response=HttpResponse()
response.write(""+FirstName+" "+LastName+u"! 你提交了表单!")
return response
else:
response=HttpResponse()
response.write('')
return response
else:
return render_to_response('formpage1.html')
def DealWithForm2(request):
if request.method=="POST":
FirstName=request.POST.get('firstname','').encode("utf-8")
LastName=request.POST.get('lastname','').encode("utf-8")
if FirstName and LastName:
html=""+FirstName+" "+LastName+"! 你提交了表单!"+""
return HttpResponse(html)
else:
response=HttpResponse()
response.write('')
return response
else:
return render_to_response('formpage2.html')
def DealWithForm3(request):
if request.method=="POST":
FirstName=request.POST.get('firstname','')
LastName=request.POST.get('lastname','')
if FirstName and LastName:
response=HttpResponse()
response.write(''+FirstName+LastName+u'! 你提交了表单!')
return response
else:
response=HttpResponse()
response.write('')
return response
else:
return render_to_response('formpage3.html')
urls.py
from django.conf.urls.defaults import patterns, include, url
import views
from django.conf import settings
urlpatterns = patterns('',
url(r'^Resource/(?P.*)$','django.views.static.serve',{'document_root':settings.STATIC_RESOURCE}),
url(r'^DealWithForm1','views.DealWithForm1'),
url(r'^DealWithForm2','views.DealWithForm2'),
url(r'^DealWithForm3','views.DealWithForm3'),
)
settings.py
# Django settings for CheckFormBeforeSubmit project.
import os
HERE = os.path.abspath(os.path.dirname(__file__))
DEBUG = True
TEMPLATE_DEBUG = DEBUG
...
STATIC_RESOURCE=os.path.join(HERE, "resource")
...
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',
'django.middleware.csrf.CsrfResponseMiddleware',
)
ROOT_URLCONF = 'CheckFormBeforeSubmit.urls'
TEMPLATE_DIRS = (
os.path.join(HERE,'template'),
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
...
在Django中,为了减轻后台压力,可以利用JavaScript在表单提交前对表单数据进行验证。下面提供了有效的几种方式(每个.html文件为一种方式)。
formpage1.html
复制代码 代码如下:
提交表单前进行验证(方法一)
formpage2.html
复制代码 代码如下:
提交表单前进行验证(方法二)
formpage3.html
复制代码 代码如下:
提交表单前进行验证(方法三)
以下是视图函数、URL配置以及相关设置
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
views.py
复制代码 代码如下:
#coding: utf-8
from django.http import HttpResponse
from django.shortcuts import render_to_response
def DealWithForm1(request):
if request.method=="POST":
FirstName=request.POST.get('firstname','')
LastName=request.POST.get('lastname','')
if FirstName and LastName:
response=HttpResponse()
response.write(""+FirstName+" "+LastName+u"! 你提交了表单!")
return response
else:
response=HttpResponse()
response.write('')
return response
else:
return render_to_response('formpage1.html')
def DealWithForm2(request):
if request.method=="POST":
FirstName=request.POST.get('firstname','').encode("utf-8")
LastName=request.POST.get('lastname','').encode("utf-8")
if FirstName and LastName:
html=""+FirstName+" "+LastName+"! 你提交了表单!"+""
return HttpResponse(html)
else:
response=HttpResponse()
response.write('')
return response
else:
return render_to_response('formpage2.html')
def DealWithForm3(request):
if request.method=="POST":
FirstName=request.POST.get('firstname','')
LastName=request.POST.get('lastname','')
if FirstName and LastName:
response=HttpResponse()
response.write(''+FirstName+LastName+u'! 你提交了表单!')
return response
else:
response=HttpResponse()
response.write('')
return response
else:
return render_to_response('formpage3.html')
urls.py
复制代码 代码如下:
from django.conf.urls.defaults import patterns, include, url
import views
from django.conf import settings
urlpatterns = patterns('',
url(r'^Resource/(?P
url(r'^DealWithForm1','views.DealWithForm1'),
url(r'^DealWithForm2','views.DealWithForm2'),
url(r'^DealWithForm3','views.DealWithForm3'),
)
settings.py
复制代码 代码如下:
# Django settings for CheckFormBeforeSubmit project.
import os
HERE = os.path.abspath(os.path.dirname(__file__))
DEBUG = True
TEMPLATE_DEBUG = DEBUG
...
STATIC_RESOURCE=os.path.join(HERE, "resource")
...
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',
'django.middleware.csrf.CsrfResponseMiddleware',
)
ROOT_URLCONF = 'CheckFormBeforeSubmit.urls'
TEMPLATE_DIRS = (
os.path.join(HERE,'template'),
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
...
下一篇: Ext 特效