【问题标题】:How to create messages if condition is True in django?如果条件在 django 中为真,如何创建消息?
【发布时间】:2022-07-21 20:19:51
【问题描述】:

我正在开发身份验证 webapp,如果用户输入与某些条件不匹配,我想显示消息。它应该类似于 this

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

我的看法:

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')

我的注册.html

{% 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 是空的。 如果您希望我发布其他任何内容,请发表评论。

【问题讨论】:

    标签: javascript python html css django


    【解决方案1】:

    您将这些错误变量传递到您的上下文中,因此您可以在模板中呈现它们,您可以执行以下操作:

    {% 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/

    【讨论】:

    • 我试过了,还是不行。我认为这是因为当我单击提交按钮时,我会得到有关错误的信息。
    • 这是因为您的代码总是重定向到电子邮件确认,如果您有错误,您应该重新呈现注册页面
    • 现在我做到了,它可以工作......有点......它不会重定向到 email_confirmation 但即使我像你那样写 if 语句,它仍然不显示消息在那里跨度>
    【解决方案2】:

    在模板中显示消息

    {% if messages %}
       <ul class="messages">
           {% for message in messages %}
             <li{% if message.tags %} class="{{ message.tags }}"
             {% endif %}>
                {{ message }}</li>
            {% endfor %}
       </ul>
     {% endif %}
    

    Docs

    【讨论】:

      猜你喜欢
      • 2013-06-08
      • 1970-01-01
      • 2021-10-13
      • 2016-01-03
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      相关资源
      最近更新 更多