当我尝试登录时,我收到一个错误,显示
duplicate key value violates unique constraint "auth_user_username_key"
DETAIL: Key (username)=(mrfrasha) already exists.
我真的完全不知道这意味着什么。这看起来很奇怪。这似乎是一个错误,你会得到它你试图创建一个已经在使用的用户名,但我只是试图登录。
<form action="" method="POST">
Username: <input type="text" name="username" />
Password: <input type="text" name="password" />
<input type = "submit" value = "Login"/>< br />
def login(request):
if request.POST=='POST':
username = request.POST['username']
password =request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return render_to_response('profile.html')
else:
print "Your account has been disabled!"#come back to me
else:
sentence = "Your username and password were incorrect."# come back to me
return render_to_response('login.html', {'sentence':sentence})
else:
return render_to_response('login.html')#come back to me
我认为的问题是,您通过声明同名函数来覆盖django login
函数,当该语句执行login(request, user)
时,该函数将变为递归函数。
由于您的函数只接受一个参数,这就是为什么login(request, user)
此语句会导致login() takes one argument and got two
的异常。
将您的函数名称更改为其他名称,例如my_login(请求)
希望这能有所帮助。感谢
已编辑
你的功能应该是这样的。
def my_login(request):
if request.method=='POST':
username = request.POST['username']
password =request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
return render_to_response('profile.html')
else:
print "Your account has been disabled!"#come back to me
else:
sentence = "Your username and password were incorrect."# come back to me
return render_to_response('login.html', {'sentence':sentence})
else:
return render_to_response('login.html')#come back to me
不完全确定这会解决问题,但这里似乎有一个问题:
if request.POST=='POST':
request.POST是字典,在该比较中永远不会计算为True。
也许可以尝试将其更改为:
if request.method == 'POST':
这至少可以让您进入代码的正确if/else部分。