正则表达式电子邮件验证django [英] regex email validation django
问题描述
在学校工作我知道如何做这个在烧瓶但学习django。我试图确保在发布之前输入有效的电子邮件。如果我不执行if else语句,表单将会通过,但不会验证它。我有以下,唯一的部分不工作是一部分的意见,但我包括整个views.py incase你看到顶部的东西可能是错误的。
Working on a project for school I know how to do this in flask but learning django. I am trying to make sure that a valid email is entered before it post. If I do not do the if else statement the form goes though but wont validate it. I have the following and the only part not working is the one part with comments but I included the whole views.py incase you see something at top that could be wrong.
views.py
from django.shortcuts import render, redirect
from .models import User
import re
EMAIL_REGEX = re.compile(r'^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9._-]+\.[a-zA-Z]+$')
# Create your views here.
def index(request):
return render(request, "emailvalidation/index.html" )
def create(request):
if request.method == 'POST': #here down
if len(request.form['email']) < 1:
add_message("Invalid Email Address!") #if i take out the if else the form does work
elif not EMAIL_REGEX.match(request.form['email']): #the validation.
add_message("Invalid Email Address!")
else:
User.objects.create(email = request.POST['email'] ) #to here
context ={
"email": User.objects.all(),
}
return render(request, 'emailvalidation/success.html' ,context)
the traceback error
Environment:
Request Method: POST
Request URL: http://localhost:8000/user
Django Version: 1.10.6
Python Version: 2.7.10
Installed Applications:
['apps.emailvalidation',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "C:\Users\dbhol\Desktop\DojoAssignments\Python\myenvirnoments\djangoENv\lib\site-packages\django\core\handlers\exception.py" in inner
42. response = get_response(request)
File "C:\Users\dbhol\Desktop\DojoAssignments\Python\myenvirnoments\djangoENv\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "C:\Users\dbhol\Desktop\DojoAssignments\Python\myenvirnoments\djangoENv\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\dbhol\Desktop\DojoAssignments\Python\myenvirnoments\django2\emailval\emailval\apps\emailvalidation\views.py" in create
15. if len(request.form['email']) < 1:
Exception Type: AttributeError at /user
Exception Value: 'WSGIRequest' object has no attribute 'form'
推荐答案
请求
没有 code>属性。由于您已经有$ code> if request.method ==POST
- 您可以安全地执行以下操作:
request
does not have a form
attribute. Since you already have if request.method == "POST"
- you can safely do the following:
更改
request.form['email']
与
request.POST['email']
理想情况下,您应该使用 django表单用于处理验证等。Django表单具有更好的验证逻辑,正则表达式验证器等。
Ideally, you should be using django forms for handing the validations etc. Django forms have better validation logic, regex validators, etc..
这篇关于正则表达式电子邮件验证django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!