繁体   English   中英

/signup UNIQUE 约束的 IntegrityError 失败:auth_user.username

[英]IntegrityError at /signup UNIQUE constraint failed: auth_user.username

我正在尝试使用 Django 创建一个身份验证系统,仅使用 3 个注册字段(用户名和 email 和密码)和 2 个登录字段(用户名和密码)我认为我得到的错误与数据库有关,我还没有使用我的模型文件,我不知道问题出在哪里,是来自注册 function 还是来自签名 function

    IntegrityError at /signup
UNIQUE constraint failed: auth_user.username
Request Method: POST
Request URL:    http://127.0.0.1:8000/signup
Django Version: 3.2.6
Exception Type: IntegrityError
Exception Value:    
UNIQUE constraint failed: auth_user.username
Exception Location: C:\Users\dell\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\base.py, line 423, in execute
Python Executable:  C:\Users\dell\AppData\Local\Programs\Python\Python39\python.exe
Python Version: 3.9.0
Python Path:    
['C:\\Users\\dell\\Desktop\\greenaftech',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python39\\DLLs',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python39\\lib',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python39',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\win32',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\win32\\lib',
 'C:\\Users\\dell\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\Pythonwin']
Server time:    Sat, 30 Apr 2022 00:24:12 +0000

这是我的 views.py

from django.shortcuts import render,redirect
from django.http import HttpResponse
from django.contrib.auth.models import User
from django.contrib import messages


# Create your views here.
def home(request):
    return render(request,"website/index.html")




def signup(request):

    if request.method=="POST":
        username=request.POST.get('username')
        password=request.POST.get('password')
        email=request.POST.get('email')
        myuser= User.objects.create_user(username,email,password)
        myuser.save()
        messages.success(request,"Your Account has been created.")
        return redirect('signin')


    return render(request,"website/signup.html")



def signin(request):
    if request.method=='POST':

        usern=request.POST.get('username')
        passs=request.POST.get('password')
        

        user=authenticate(username=usern,password=passs)
        if user is not None:
            login(request,user)
            userr=user.username
            return render(request,"website/index.html",{'username':userr})
        else:
            messages.error(request,"Wrong Infos")
            return redirect('home') 

    return render(request,"website/signin.html")



def signout(request):
    pass    

这些是我的 URLs.py

from django.contrib import admin
from django.urls import path,include
from . import views

urlpatterns = [
   path('',views.home,name='home'),
   path('signup',views.signup,name='signup'),
   path('signin',views.signin,name='signin'),
   path('signout',views.signout,name='signout'),
]

如果您尝试创建两个具有相同用户名的用户,并且您的 uath_user 模型的用户名字段具有 unique=True 约束(这会阻止两个记录在该字段中具有相同的值),则会发生该错误。

请注意,如果您使用 User.objects.create(...) 而不是 User(username="username"...) 它将自动保存新记录,因此 myUser.save() 可能会尝试复制该创建,创建约束问题,同时仍允许您在管理员中查看该项目

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM