尝试登录时如何解决完整性错误(Django) [英] How to resolve an integrity error when trying to log in. (Django)

查看:257
本文介绍了尝试登录时如何解决完整性错误(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


推荐答案

我认为是通过声明sam的功能来覆盖django 登录函数e名称,然后当该语句执行 login(request,user)时,递归。

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).

随着您的功能需要只有一个参数是为什么 login(request,user)此语句导致异常, login()接受一个参数,并获得两个

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.

将您的函数名称更改为其他例如my_login(request)

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

这篇关于尝试登录时如何解决完整性错误(Django)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆