0

I'm recently trying to get back into Django, but am having some issues trying to make a login page for practice. I'm getting the error "login takes exactly 1 argument (2 given)" and I have no idea why. Here is my views.py:

from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login
from django.template import RequestContext

def login(request):
    state = "Please log in below..."
    username = password = ''
    if request.POST:
        username = request.POST.get('username')
        password = request.POST.get('password')

        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                state = "You're successfully logged in!"
            else:
                state = "Your account is not active, please contact the site admin."
        else:
            state = "Your username and/or password were incorrect."

    return render_to_response('login.html',{'state':state, 'username': username},context_instance=RequestContext(request))

and here is my login.html template:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Log in</title>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<style>
body{
    font-family:Arial,Helvetica,sans-serif;
    font-size: 12px;
}
</style>
</head>
<body>
    {{ state }}
    <form action="/login/" method="post">
        {% csrf_token %}
        {% if next %}
        <input type="hidden" name="next" value="{{ next }}" />
        {% endif %}
        Username:
        <input type="text" name="username" value="{{ username }}" /><br />
        Password:
        <input type="password" name="password" value="" /><br />

        <input type="submit" value="Log In" />
    </form>
</body>
</html>

Thanks for any help.

  • See here: http://stackoverflow.com/questions/4547639/django-csrf-verification-failed – favoretti Jan 01 '13 at 15:28
  • Basically you need a `csrf_token` in template and `RequestContext` in `render_to_response` – favoretti Jan 01 '13 at 15:29
  • @favoretti Okay, I just moved the RequestContext into the render_to_response function, and now I get an error while trying to login that says "login() takes exactly 1 argument (2 given)" – Konstantin Keller Jan 01 '13 at 16:09
  • Can you edit your code in question to reflect your last change? – favoretti Jan 01 '13 at 16:12
  • OTOH, if you aren't concerned with the CSRF stuff, you can just mark your `login()` method with a `@csrf_excempt` decorator and be done with it :) – favoretti Jan 01 '13 at 16:13
  • @favoretti Just updated the code and question. I would rather learn the csrf stuff incase I need it for an actual site in the future though. Also, I think the new error has nothing to do with csrf, rather the actual view. Here is the actual error: http://pastebin.com/ySfqpVKc – Konstantin Keller Jan 01 '13 at 16:19
  • Which Django version are you using? If you use 1.4, you can also mark the method with `@csrf_token` decorator, as per 2nd answer in the question I posted. – favoretti Jan 01 '13 at 16:26
  • Also, http://stackoverflow.com/questions/5689714/django-form-takes-exactly-1-argument-2-given-error-possibly-related-to-cs might be your case :) – favoretti Jan 01 '13 at 16:28
  • Also check this solution http://stackoverflow.com/questions/9594077/how-to-resolve-an-integrity-error-when-trying-to-log-in-django/ – Aamir Rind Jan 01 '13 at 16:38

1 Answers1

14

You should change the name of the view, from login to something else, e.g. login_view, because you currently trying to call it recursively, instead of calling django.contrib.auth.login

rob
  • 36,896
  • 2
  • 55
  • 65