惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
Schneier on Security
Schneier on Security
V
V2EX - 技术
S
Secure Thoughts
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
C
CERT Recently Published Vulnerability Notes
NISL@THU
NISL@THU
S
Securelist
S
Security Archives - TechRepublic
Know Your Adversary
Know Your Adversary
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
Recent Commits to openclaw:main
Recent Commits to openclaw:main
G
GRAHAM CLULEY
H
Hacker News: Front Page
Microsoft Azure Blog
Microsoft Azure Blog
I
Intezer
Google Online Security Blog
Google Online Security Blog
美团技术团队
阮一峰的网络日志
阮一峰的网络日志
T
The Exploit Database - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Webroot Blog
Webroot Blog
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
P
Proofpoint News Feed
The Cloudflare Blog
I
InfoQ
L
LangChain Blog
U
Unit 42
P
Proofpoint News Feed
S
Schneier on Security
S
Security Affairs
Y
Y Combinator Blog
T
Tenable Blog
N
News and Events Feed by Topic
MyScale Blog
MyScale Blog
量子位
Google DeepMind News
Google DeepMind News
Cyberwarzone
Cyberwarzone
博客园 - 聂微东
D
Darknet – Hacking Tools, Hacker News & Cyber Security
GbyAI
GbyAI
AWS News Blog
AWS News Blog

Django - 标签 - cywhat's blog

Python创建并上传自己的pip依赖包 Django操作异步任务 ValueError:invalid UnorderedObjectListWarning:Pagination May Yield Inconsistent Results With an Unordered Object_list Django删除表重建 Django的csrf防御机制 Django连接Mysql配置 RuntimeWarning:DateTimeField Session.expire_date received a naive datetime fonts.googleapis.com访问较慢[已解决]
Django中的form表单校验
cywhat · 2021-09-25 · via Django - 标签 - cywhat's blog

前景:

我在使用django的form组件时,发现在view函数中的`form.is_valid()`在form表单校验未通过的情况下,返回的仍然是True,最后发现还是form表单的问题,异常函数并没有传递给view函数

问题代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
form部分


def clean_email(self):
    """
    邮箱校验
    :return:
    """
    email_title = "验证码"
    code = random.randrange(1000, 9999)
    context = {
        'code': str(code)
    }
    email_template_name = 'tools_email.html'
    t = loader.get_template(email_template_name)
    html_content = t.render(context)
    email = self.cleaned_data['email']
    msg = EmailMessage(email_title,  
                       html_content,  
                       settings.EMAIL_FROM,  
                       [email]
                       )
    msg.content_subtype = 'html'
    exists = models.UserInfo.objects.filter(email=email).exists()
    if not exists:
        return ValidationError('邮箱未注册')   #注意就是这个异常函数ValidationError,没有将异常传给view
    send_status = msg.send()
    if not send_status:
        self.add_error("email", '发送邮箱失败,{}'.format(send_status['errmsg']))
    conn = get_redis_connection()
    conn.set(email, code, ex=120)
    return email
1
2
3
4
5
6
7
view部分

def send_sms(request):
    form = SendEmailSmsForm(request, data=request.POST)
    if form.is_valid():     #这里接收form传过来的异常,如果有异常则返回false,反之true
        return JsonResponse({'status': True})
    return JsonResponse({'status': False, 'error': form.errors})

原因:

我也不知道为什么 ValidationError 为什么没有把异常抛给view 更换另一个抛出异常的函数即可

解决:

1
2
3
4
5
return ValidationError('邮箱未注册')   #改为


self.add_error("email", "邮箱未注册")   # email为异常参数的field "邮箱未注册" 为报错文案

关注一下再走吧

公众号 小程序

赞赏支持

微信打赏 支付宝打赏