Home > Web Front-end > JS Tutorial > Several ways to verify before submitting the form_javascript skills

Several ways to verify before submitting the form_javascript skills

WBOY
Release: 2016-05-16 17:27:33
Original
1106 people have browsed it

Several ways to verify the form before submitting it.
In Django, in order to reduce background pressure, JavaScript can be used to verify the form data before the form is submitted. Several valid methods are provided below (one method for each .html file).
formpage1.html

Copy code The code is as follows:





Example1





提交表单前进行验证(方法二)














first_name:
last_name:









formpage3.html
复制代码 代码如下:





Example3




提交表单前进行验证(方法三)














first_name:
last_name:









以下是视图函数、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 및 LastName:
응답 =HttpResponse()
response.write("" FirstName " " LastName u"! 你提交了表单!")
응답 반환
그 외:
response=HttpResponse()
response.write('')
응답 반환
else:
return render_to_response('formpage1.html')
def DealWithForm2 (요청):
if request.method=="POST":
FirstName=request.POST.get('firstname','').encode("utf-8")
LastName=request .POST.get('lastname','').encode("utf-8")
FirstName 및 LastName:
html="" 이름 " " 성 "! 你提交了表单!" ""
HttpResponse(html) 반환
else:
response=HttpResponse()
response.write('')
응답 반환
else:
return render_to_response('formpage2. html')
def DealWithForm3(요청):
if request.method=="POST":
FirstName=request.POST.get('firstname','')
LastName=request. POST.get('lastname','')
if FirstName 및 LastName:
response=HttpResponse()
response.write('' FirstName LastName u'! 你提交了表单!')
응답 반환
else:
response=HttpResponse()
response.write('<스크립트 유형 ="text/javascript">alert("firstname或lastname不能为空!")
window.location="/DealWithForm3"')
응답 반환
else:
return render_to_response('formpage3.html')

urls.py
复system代码 代码如下:

from django.conf.urls.defaults 가져오기 패턴, 포함, URL
보기 가져오기
django.conf 가져오기 설정
urlpatterns = 패턴 ('',
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
复代码 代码如下:

# CheckFormBeforeSubmit 프로젝트에 대한 Django 설정입니다.
os 가져오기
여기 = os.path.abspath(os.path.dirname(__file__))
DEBUG = True
TEMPLATE_DEBUG = DEBUG
...
STATIC_RESOURCE=os. path.join(여기, "리소스")
...
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'),
# 여기에 문자열을 입력하세요. "/home/html/django_templates" 또는 "C:/www/django/templates"와 같습니다.
# Windows에서도 항상 슬래시를 사용하세요.
# 상대 경로가 아닌 절대 경로를 사용하는 것을 잊지 마세요. .
)
...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template