首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果django中的条件为True,如何创建消息?

如果django中的条件为True,如何创建消息?
EN

Stack Overflow用户
提问于 2022-07-21 10:15:36
回答 1查看 71关注 0票数 0

我正在做认证webapp,如果用户输入不符合某些条件,我想显示消息。它应该看起来像

例如:如果密码长度小于8个字符,则显示消息:“密码应大于8个字符!”

My view:

代码语言:javascript
复制
def signup(request):
    if request.method == "POST":
        context = {'has_error': False,
        'data': request.POST,
        'length_error': False,
        'password_match_error': False,
        'validate_email_error': False,
        }
        global password
        global password2
        global email
        global username
        email = request.POST.get('email')
        username = request.POST.get('username')
        password = request.POST.get('password')
        password2 = request.POST.get('password2')

        if len(password) < 8:
                ############custom message
                context['length_error'] = True

        if password != password2:
            ############custom message
            context['password_match_error'] = True

        if not validate_email(email):
            ############custom message
            context['validate_email_error'] = True

        if not username:
            ############custom message
            context['has_error'] = True

        if models.CustomUser.objects.filter(email=email).exists():
            messages.add_message(request, messages.ERROR, 'This email is already registered!')
            context['has_error'] = True

            return render(request, 'authentication/signup.html', context, status=409)
        if context['has_error']:
            return render(request, 'authentication/signup.html', context)

        body = render_to_string('authentication/email/email_body.html', {
            'username': username,
            'token': token,
        })
        send_mail(
            "Email Confirmation",
            body,
            'tadejtilinger@gmail.com',
            [email]
        )
        return redirect('email-confirmation')
    return render(request, 'authentication/signup.html')

My signup.html

代码语言:javascript
复制
{% include  "_base.html" %}
{% load static %}

{% block title %}Sign Up{% endblock title %}

{% block content %}
<link rel="stylesheet" href="{% static 'css/authentication/signup.css' %}">
<div class="container">
  <form class="signup_form" method="post" action="{% url 'signup' %}">
    {% csrf_token %}
    <input type="text" placeholder="Email" class="input_1" name="email" value="{{ data.email }}">
    <input type="text" placeholder="Username" class="input_2" name="username" value="{{ data.username }}">
    <input type="text" placeholder="Password" class="input_3" name="password" value="{{ data.password }}">
    <input type="text" placeholder="Confirm password" class="input_4" name="password2" value="{{ data.password2 }}">
    <button type="submit" class="submit_btn1">Sign Up</button>
  </form>
</div>
{% block scripts %}
<script src="js/signup.js"></script>
{% endblock scripts %}
{% endblock content %}

我的signup.js是空的。如果你想让我发其他的东西,就评论吧。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-21 10:29:18

您要将这些错误变量传递到上下文中,这样您就可以在模板中呈现它们,您可以这样做:

代码语言:javascript
复制
{% if length_error %}
<p>Your password must be longer than 8 characters</p>
{% endif %}

但对于你所有不同的错误。Django有一个内置的登录表单,它可以验证这类事情,我建议使用它而不是重新发明轮子。

https://docs.djangoproject.com/en/4.0/topics/auth/default/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73064392

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档