简体   繁体   English

Django-用户身份验证错误(django.contrib.auth)

[英]Django - user authentification error (django.contrib.auth)

Following this tutorial I have created a simple login page. 在学习完本教程之后,我创建了一个简单的登录页面。 But when I submit the login information, following exception in Python appears: 但是当我提交登录信息时,Python中出现以下异常:

Traceback (most recent call last): File "/home/jirka/miniconda3/envs/molinf/lib/python3.5/site-packages/django/core/handlers/base.py", line 149, in get_response response = self.process_exception_by_middleware(e, request) File "/home/jirka/miniconda3/envs/molinf/lib/python3.5/site-packages/django/core/handlers/base.py", line 147, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/jirka/molinf/02-database/moldb/views.py", line 100, in login login(user) File "/home/jirka/molinf/02-database/moldb/views.py", line 94, in login if request.POST: AttributeError: 'User' object has no attribute 'POST'

Here is my code: 这是我的代码:

URL in urls.py : url(r'^login$', moldb.views.login, name='login') urls.py URL: url(r'^login$', moldb.views.login, name='login')

Login view in views.py : views.py登录视图:

def login(request):
    print(request, type(request))
    logout(request)

    if request.POST:
        username = request.POST['username']
        password = request.POST['password']

        user = authenticate(username=username, password=password)
        if user is not None and user.is_active:
            login(user)
            return HttpResponseRedirect('/')

    return render(request, "login.html")

Login form in login.html : login.html登录表单:

<form class="form-horizontal" name="LoginForm" action="{% url 'login' %}" method="post">
    {% csrf_token %}
    {% if next %}
        <input type="hidden" name="next" value="{{ next }}" />
    {% endif %}
    <div class="control-group">
        <label class="control-label" for="username">Username</label>
        <div class="controls">
        <input type="text" id="username" name="username" placeholder="Username">
        </div>
    </div>
    <div class="control-group">
        <label class="control-label" for="password">Password</label>
        <div class="controls">
        <input type="password" name="password" id="password" placeholder="Password">
        </div>
    </div>
    <div class="control-group">
        <div class="controls">
        <button type="submit" class="btn">Login</button>
        </div>
    </div>
</form>

I have found that output from print(request, type(request)) in views.py is following after submitting the form: 我发现提交表单后,在views.py print(request, type(request))输出如下:

<WSGIRequest: POST '/login'> <class 'django.core.handlers.wsgi.WSGIRequest'>
hanicka <class 'django.contrib.auth.models.User'>

hanicka is the username of authenticated user (the username which I sent through the login form). hanicka是经过身份验证的用户的用户名(我通过登录表单发送的用户名)。 It is obvious that the exception comes from this, but how is this view executed twice and how is the User model getting here?! 很明显,异常是由此产生的,但是该视图如何执行两次, User模型如何到达这里?

I am not familiar with Django and I'm missing some context, but just looking at your code, the example you followed and the Django docs, I am curious what would happen if you chose a different name of your login function to: 我不熟悉Django,并且缺少一些上下文,但是仅查看您的代码,您遵循的示例和Django文档,我很好奇如果您选择其他login函数名称会发生​​什么情况:

def login_user(request):

Just in case there are naming clashes... 以防万一命名冲突...

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

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