【发布时间】:2023-01-08 03:08:44
【问题描述】:
我正在为我的网站创建一个用户注册页面,我正在尝试为我的表单创建自定义验证字段。我试图让验证错误显示在网页上,但我似乎无法让它们显示。我很确定它与 HTML 部分有关,但我是新手并且不确定。以下是我的文件设置
表单.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
模型.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
视图.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 此密码不包含大写字符 密码123 [07/Jan/2023 16:36:08]“POST /accountSetup/HTTP/1.1”200 3906
我不确定为什么这会显示在获取响应而不是帖子中:(
【问题讨论】:
-
删除这个
form = createUser()否则它应该可以工作。 -
form = createUser()应该在if request.method == 'POST':的else块中,而不是在if form.is_valid():的else块中。