简体   繁体   中英

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:

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

Login view in 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 :

<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:

<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). It is obvious that the exception comes from this, but how is this view executed twice and how is the User model getting here?!

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:

def login_user(request):

Just in case there are naming clashes...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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