错误不会出现,使用 Django 进行表单验证

Error won't showing up, Form validation with Django

我刚开始为我的新论坛项目学习 Django,我试图自己找出它,但这个问题已经困扰我 2 天了。我正在尝试对我的表单进行注册验证,我正在使用 messages.error(request, "Text") 使屏幕上出现错误,但它不起作用,什么也没显示。

这是我的 HTML 代码的一部分:

    <div class="limiter">
        <div class="container-login100" style="background:black;">
            <div class="wrap-login100">
                <form class="login100-form validate-form"  method='POST' action="{% url 'users:register' %}" >
                    {% csrf_token %}
                    <span class="login100-form-logo">
                        <i class="zmdi zmdi-landscape"></i>
                    </span>
    
                    <span class="login100-form-title p-b-34 p-t-27">
                        Register
                    </span>
    
                    <div class="wrap-input100 validate-input" data-validate = "Enter username">
                        <input class="input100" type="text" name="username" placeholder="Username" required>
                        <span class="focus-input100" data-placeholder="&#xf207;"></span>
                    </div>
    
                    <div class="wrap-input100 validate-input" data-validate="Enter password">
                        <input class="input100" type="password" name="pass" placeholder="Password" required>
                        <span class="focus-input100" data-placeholder="&#xf191;"></span>
                    </div>
    
                    <div class="wrap-input100 validate-input" data-validate="Confirm password">
                        <input class="input100" type="password" name="pass-confirm" placeholder="Confirm Password" required>
                        <span class="focus-input100" data-placeholder="&#xf191;"></span>
                    </div>
    
                    <div class="wrap-input100 validate-input" data-validate="Enter Email">
                        <input class="input100" type="email" name="mail" placeholder="E-Mail" required>
                        <span class="focus-input100" data-placeholder="&#xf191;"></span>
                    </div>
    
                    <div class="container-login100-form-btn">
                        <button class="login100-form-btn">
                            Register
                        </button>
                    </div>
    
                    <div class="text-center p-t-90">
                        <a class="txt1" href="login">
                            Already registered?
                        </a>
                    </div>
    
                </form>
            </div>
        </div>
    </div>

这是我的 views.py 注册函数:

def register(request):
    if request.method == "POST":
        username = request.POST["username"]
        password = request.POST["pass"]
        password_confirm = request.POST["pass-confirm"]
        email = request.POST["mail"]

        
        print("UserName : ", username)
        print('Email : ', email)
        print('Password : ', password)
        print('Password Confirm : ', password_confirm)

        if len(username) < 7:
            # print works, so I know that these POST variables are successfully transmitted.
            print('Username must be more than 10 char.')
            messages.error(    #messages.error won't show up. 
                request, "Username must be more than 10 char.", 'red')
            return HttpResponseRedirect(reverse('users:register'))
        
        if len(password) < 8:
            print("Your password is too short")
            messages.error(request, "Your password is too short.", 'red')
            return HttpResponseRedirect(reverse('users:register'))
        
        if (password != password_confirm):
            print("Passwords don't match")
            messages.error(request, "Passwords don't match.", 'red')
            return HttpResponseRedirect(reverse('users:register'))
        
        else:
            messages.success(request, "Success! form submitted.", 'green')
            return HttpResponseRedirect(reverse('users:register'))
        

    return render(request, 'users/register.html')

messages.error 的样式:

.green{
  color:green;
  font-size:1.3rem;
}
.red{
  color:red;
  font-size:1.3rem;
}

如果我这样做完全错误,请告诉我,我没有使用 Django 的 POST API,因为我不知道如何将我的自定义样式应用于它。

django-doc开始,可以使用简单的渲染格式messages

尝试以下格式的消息呈现。

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

在 html 文件的顶部或您想要的任何位置添加上述消息呈现格式:

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

  <div class="limiter">
        <div class="container-login100" style="background:black;">
            <div class="wrap-login100">
                <form class="login100-form validate-form"  method='POST' action="{% url 'users:register' %}" >
                    {% csrf_token %}
                    <span class="login100-form-logo">
                        <i class="zmdi zmdi-landscape"></i>
                    </span>
    
                    <span class="login100-form-title p-b-34 p-t-27">
                        Register
                    </span>
    
                    <div class="wrap-input100 validate-input" data-validate = "Enter username">
                        <input class="input100" type="text" name="username" placeholder="Username" required>
                        <span class="focus-input100" data-placeholder="&#xf207;"></span>
                    </div>
    
                    <div class="wrap-input100 validate-input" data-validate="Enter password">
                        <input class="input100" type="password" name="pass" placeholder="Password" required>
                        <span class="focus-input100" data-placeholder="&#xf191;"></span>
                    </div>
    
                    <div class="wrap-input100 validate-input" data-validate="Confirm password">
                        <input class="input100" type="password" name="pass-confirm" placeholder="Confirm Password" required>
                        <span class="focus-input100" data-placeholder="&#xf191;"></span>
                    </div>
    
                    <div class="wrap-input100 validate-input" data-validate="Enter Email">
                        <input class="input100" type="email" name="mail" placeholder="E-Mail" required>
                        <span class="focus-input100" data-placeholder="&#xf191;"></span>
                    </div>
    
                    <div class="container-login100-form-btn">
                        <button class="login100-form-btn">
                            Register
                        </button>
                    </div>
    
                    <div class="text-center p-t-90">
                        <a class="txt1" href="login">
                            Already registered?
                        </a>
                    </div>
    
                </form>
            </div>
        </div>
    </div>