ValueError:用户名必须设置(Django)

ct2axkht  于 2023-06-25  发布在  Go
关注(0)|答案(3)|浏览(184)

我正在尝试在Django中实现用户身份验证,但是每次我尝试运行服务器时,我都会得到这个错误“ValueError:必须设置给定的用户名“错误消息强调了设置特定用户名的必要性。尽管作出了不懈努力,但这一困境仍难以得到令人满意的解决。令人遗憾的是,详尽的故障排除和调查尚未产生可行的解决方案。代码如下:

注册.html

<html>
<head>
    <title>Signup</title>
    <style>
        body {
            background-color: #222;
            color: #fff;
            font-family: Arial, sans-serif;
        }

        .container {
            max-width: 400px;
            margin: 0 auto;
            padding: 20px;
            box-sizing: border-box;
            background-color: #333;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
        }

        h1 {
            text-align: center;
            margin-bottom: 30px;
        }

        input[type="text"],
        input[type="email"],
        input[type="password"] {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            background-color: #444;
            border: none;
            border-radius: 4px;
            color: #fff;
        }

        input[type="submit"] {
            width: 100%;
            padding: 10px;
            background-color: #f44336;
            border: none;
            border-radius: 4px;
            color: #fff;
            cursor: pointer;
        }

        input[type="submit"]:hover {
            background-color: #e53935;
        }

        ::placeholder {
            color: #888;
        }
    </style>
</head>
<body>
    
    <form method="post" action="home/">
        {% csrf_token %}
            <input type="text" name="username" placeholder="Username" required/>
            <input type="email" name="email" placeholder="Email" required/>
            <input type="password" name="password" placeholder="Password" required/>
            <input type="password" name="confirm_password" placeholder="Confirm Password" required/>
            <input type="submit" name="submit" value="Sign Up"/>
    </form>
</body>
</html>

views.py

from django.shortcuts import render
from django.http import HttpResponse
from django.contrib.auth.models import User
def signup(request):
    username=request.POST.get('username')
    email=request.POST.get('email')
    password=request.POST.get('password')
    confirm_password=request.POST.get('confirm_password')
    the_user=User.objects.create_user(username=username,email=email,password=password)
    the_user.save()
    return render(request,'signup.html')

def home(request):
    return HttpResponse('Login Successfull')
lzfw57am

lzfw57am1#

请检查username变量是否为空:

def signup(request):
    print(request.POST)
    username=request.POST.get('username')

检查请求的正文,查看实际发送到服务器的内容。这样,您就可以查看用户名是否为空。如果用户名为空,则意味着您的表单有问题。

q43xntqr

q43xntqr2#

也许扩展UserCreationForm会更好。下面是一个例子:https://simpleisbetterthancomplex.com/tutorial/2017/02/18/how-to-create-user-sign-up-view.html#sign-up-with-extra-fields

emeijp43

emeijp433#

首先,您的表单有一个action="home/",这是表单信息发送到的位置。其次,当用户转到signup视图填写表单时,您会得到错误,因为表单尚未填写!你得先检查一下这个。

解决方案

首先检查用户是否正在发送表单,而不是只是去URL填写表单:

def signup(request):
    if request.method == 'POST':
        username=request.POST.get('username')
        email=request.POST.get('email')
        password=request.POST.get('password')
        confirm_password=request.POST.get('confirm_password')
        the_user=User.objects.create_user(username=username,email=email,password=password)
        the_user.save()
    return render(request,'signup.html')

第二,要么使用Django的url作为你的action,要么把action从表单中删除。这会将表单发送回创建页面的视图:

<form method="post">
    {% csrf_token %}
    <input type="text" name="username" placeholder="Username" required/>
    <input type="email" name="email" placeholder="Email" required/>
    <input type="password" name="password" placeholder="Password" required/>
    <input type="password" name="confirm_password" placeholder="Confirm Password" required/>
    <input type="submit" name="submit" value="Sign Up"/>
</form>

最后,用户创建和身份验证是一件常见的事情,有很好的现成库,包括Django's own user authentication system(尽管这对用户创建没有帮助,只是身份验证),或者更好地使用django-allauth

相关问题