Django验证错误没有显示在网页上,但在控制台被读取和显示



我正在为我的网站创建一个用户注册页面,我正在尝试为我的表单定制验证字段。我试图得到验证错误显示在网页上,但我似乎不能让他们显示。我很确定这与HTML部分有关,但我是新手,不确定。下面是我的文件

的设置forms.py

from django import forms

class createUser(forms.Form):
username = forms.CharField(
label='username',
max_length=30,
widget=forms.TextInput(attrs={'class': "input"}),
required=True
)
password = forms.CharField(
label='password',
max_length=30,
widget=forms.TextInput(attrs={'class': "input"}),
required=True
)
password2 = forms.CharField(
label='password2',
max_length=30,
widget=forms.TextInput(attrs={'class': "input"}),
required=True
)
email = forms.CharField(
label='email',
max_length=50,
widget=forms.TextInput(attrs={'class': "input"}),
required=True
)
canvas_token = forms.CharField(
label='token',
widget=forms.TextInput(attrs={'class': "input"}),
required=True
)

def clean_password(self):
password = self.cleaned_data.get('password')
if not any(char.isupper() for char in password):
raise forms.ValidationError("This password does not contain an uppercase character")
return password

models.py

from django.db import models

# Create your models here.
class Auth(models.Model):
username = models.CharField(max_length=20, blank=False, null=False)
password = models.CharField(max_length=150, blank=False, null=False)
email_address = models.TextField(max_length=50, blank=False, null=False)
canvas_token = models.TextField(blank=False, null=False)
def __str__(self):
# this will name the object entry within the database to the username that was entered
return self.username

views.py

from django.shortcuts import render, HttpResponseRedirect
from django.http import HttpResponse
from .forms import createUser
from .models import Auth
from django.contrib.auth.forms import UserCreationForm

# Create your views here.
def login_page(request, *args, **kwargs):
return render(request, "login.html", {})
# Show the login page (username, password)

def account_setup(request, *args, **kwargs):
# check if the request is post
form = ""
if request.method == 'POST':
form = createUser(request.POST)
if form.is_valid():
# if the form is valid, the user data will be saved and the user will be redirected back to the login page
username = request.POST['username']
password = request.POST['password']
password2 = request.POST['password2']
email = request.POST['email']
instance = Auth(username=username, email_address=email, password=password)
instance.save()
print("data has been saved to DB")
return HttpResponseRedirect('/')
else:
# if the form is not valid the user will be redirected to the same page but the validation errors will be displayed
form = createUser()
context = {'form': form}
return render(request, "accountSetup.html", context)

正在显示的html页面

<!DOCTYPE html>
<html lang="en">
<head>
<title>Cheat Checker</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->
{% load static %}
<link rel="icon" type="image/png" href="{% static 'login_page/images/icons/favicon.ico' %}"/>
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{% static 'login_page/fonts/font-awesome-4.7.0/css/font-awesome.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{% static 'login_page/fonts/iconic/css/material-design-iconic-font.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{% static 'login_page/vendor/animate/animate.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{% static 'login_page/vendor/css-hamburgers/hamburgers.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{% static 'login_page/css/util.css' %}">
<link rel="stylesheet" type="text/css" href="{% static 'login_page/css/main.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<div class="limiter">
<div class="container-login100" style="background-image: url('../static/login_page/images/bg-01.jpg');">
<div class="wrap-login100">
<form class="login100-form validate-form" method="POST"> {%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">
Create Account
</span>
<div class="wrap-input100 validate-input" data-validate = "Enter username">
<input class="logininput100" type="text" name="username" placeholder="Username" required minlength="6">
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input" data-validate="Enter password">
<input class="logininput100" type="password" name="password" placeholder="Password" required minlength="8">
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input" data-validate="Re-enter password">
<input class="logininput100" type="password" name="password2" placeholder="Retype Password" required minlength="8">
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input" data-validate="Enter Email">
<input class="logininput100" type="email" name="email" placeholder="Email">
<span class="focus-input100"></span>
</div>
<div class="wrap-input100 validate-input" data-validate="Enter Canvas Token">
<input class="logininput100" type="text" name="canvas_token" placeholder="Canvas Token">
<span class="focus-input100"></span>
</div>
<div class="container-login100-form-btn">
<button class="login100-form-btn" type="submit">
Create Account
</button>
</div>
<div class="text-center p-t-10">
<p class="create_account">
Already have an account?
<a class="create_account" href="{% url 'login' %}">
Click here to login
</a>
</p>
</div>
</form>
</div>
</div>
</div>

控制台输出,显示正在读取错误但未显示

[07/Jan/2023 16:35:49] "GET/accountSetup/HTTP/1.1"200 3906此密码不包含大写字符password123[07/Jan/2023 16:36:08] POST/accountSetup/HTTP/1.1"200 3906我不知道为什么这是显示在get响应而不是post上:(

你必须像这样在模板上显示表单:

{{ form }}

和错误,你可以添加以下代码到你的模板:

{{ form.errors }}

{{ form.password.errors }} 

我建议你在做所有这些事情之前先阅读一下使用表单的文档。

我希望这对你有帮助。

最新更新