How to resolve an integrity error when you try to connect. (Django)
source link: https://www.codesd.com/item/how-to-resolve-an-integrity-error-when-you-try-to-connect-django.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
How to resolve an integrity error when you try to connect. (Django)
when I try to log in i get an error that says
duplicate key value violates unique constraint "auth_user_username_key"
DETAIL: Key (username)=(mrfrasha) already exists.
I really don't have any idea what this means at all. It seems weird. this seems like an error you would get it you were trying to create a username that was already in use but i merely trying to log in.
<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
The problem which i think is that you have override the django login
function by declaring the function of same name which then becomes recursive when this statement will execute login(request, user)
.
As your function takes only one parameter that is why login(request, user)
this statement cause exceptions that login() takes one argument and got two
.
Change your function name to some other e.g. my_login(request)
Hope this helps. Thanks
EDITED
Your function should be like this.
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
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK