0

I'm working in Django for the first time and so far it's been hopping from error to error, which is really forcing me to pay attention to what I'm doing. However there's this issue which has been bugging me quite a bit.

I'm getting the following error in my browser:

TypeError at /login/
login() takes exactly 2 arguments (1 given)
Request Method: POST
Request URL:    http://127.0.0.1:8000/login/
Django Version: 1.6
Exception Type: TypeError
Exception Value:    
login() takes exactly 2 arguments (1 given)

When I take a look at the code in my views.py that takes care of my authenticate() and login() functions, I've got the following code:

@cache_page(60 * 15)
def login_user(request):
    context_instance=RequestContext(request)
    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('undercovercoders/index.html', {'state':state, 'username':username}, context_instance=RequestContext(request))

I was using the official docs to create the if/else loop up there, but why is is not being defined? Is it because the authenticate() is returning nothing? Shouldn't that mean the website returns the error state? Thank you so much for helping, let me know if there's anything I can add!

Edit

My urls.py

from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import *

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

    urlpatterns = patterns('',
    (r'^/$','portal.views.index'),
    (r'^$','portal.views.index'), 

 # Login / logout.

(r'^registration/$', 'portal.views.registration'),
(r'^login/$', 'portal.views.login'),
(r'^dashboard/$', 'portal.views.dashboard'),
(r'^team/$', 'portal.views.team'),
(r'^about/$', 'portal.views.about'),
(r'^parents/$', 'portal.views.parents'),
(r'^legal/$', 'portal.views.legal'),
(r'^index/$', 'portal.views.index'),
4

1 回答 1

2

看看你有什么:

(r'^login/$', 'portal.views.login'),

但是你的观点叫做login_user.

将上面的行替换为

(r'^login/$', 'portal.views.login_user'),

您的/login/网址实际上是在调用该django.auth.login方法而不是您的视图。

于 2013-08-08T03:45:14.437 回答