Several ways to verify before submitting the form_javascript skills
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
Verify before submitting the form (Method 1)
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 및 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
from django.conf.urls.defaults 가져오기 패턴, 포함, URL
보기 가져오기
django.conf 가져오기 설정
urlpatterns = 패턴 ('',
url(r'^Resource/(?P
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에서도 항상 슬래시를 사용하세요.
# 상대 경로가 아닌 절대 경로를 사용하는 것을 잊지 마세요. .
)
...

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











1. After opening WeChat, click the search icon, enter WeChat team, and click the service below to enter. 2. After entering, click the self-service tool option in the lower left corner. 3. After clicking, in the options above, click the option of unblocking/appealing for auxiliary verification.

Layui login page jump setting steps: Add jump code: Add judgment in the login form submit button click event, and jump to the specified page through window.location.href after successful login. Modify the form configuration: add a hidden input field to the form element of lay-filter="login", with the name "redirect" and the value being the target page address.

layui provides a variety of methods for obtaining form data, including directly obtaining all field data of the form, obtaining the value of a single form element, using the formAPI.getVal() method to obtain the specified field value, serializing the form data and using it as an AJAX request parameter, and listening Form submission event gets data.

There are the following methods for front-end and back-end interaction using layui: $.ajax method: Simplify asynchronous HTTP requests. Custom request object: allows sending custom requests. Form control: handles form submission and data validation. Upload control: easily implement file upload.

Steps to build a single-page application (SPA) using PHP: Create a PHP file and load Vue.js. Define a Vue instance and create an HTML interface containing text input and output text. Create a JavaScript framework file containing Vue components. Include JavaScript framework files into PHP files.

In Vue.js, event is a native JavaScript event triggered by the browser, while $event is a Vue-specific abstract event object used in Vue components. It is generally more convenient to use $event because it is formatted and enhanced to support data binding. Use event when you need to access specific functionality of the native event object.

PHP8 is the latest version of PHP, bringing more convenience and functionality to programmers. This version has a special focus on security and performance, and one of the noteworthy new features is the addition of verification and signing capabilities. In this article, we'll take a closer look at these new features and their uses. Verification and signing are very important security concepts in computer science. They are often used to ensure that the data transmitted is complete and authentic. Verification and signatures become even more important when dealing with online transactions and sensitive information because if someone is able to tamper with the data, it could potentially

Servlet serves as a bridge for client-server communication in Java Web applications and is responsible for: processing client requests; generating HTTP responses; dynamically generating Web content; responding to customer interactions; managing HTTP session state; and providing security protection.
