我第一次在Django工作,到目前为止,它一直在从错误跳到错误,这真的迫使我注意我正在做的事情。然而,这个问题一直困扰着我。
我的浏览器出现以下错误:
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)
当我查看负责authenticate()和login()函数的 views.py 中的代码时,我得到了以下代码:
@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))
我正在使用官方文档在那里创建if / else循环,但为什么没有定义?是因为authenticate()没有返回任何内容吗?这不应该意味着网站返回错误状态?非常感谢您的帮助,请告诉我是否有任何可以添加的内容!
修改
我的 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'),
答案 0 :(得分:2)
了解你的情况:
(r'^login/$', 'portal.views.login'),
但您的观点称为login_user
。
用
替换上面的行(r'^login/$', 'portal.views.login_user'),
您的/login/
网址实际上是在调用django.auth.login
方法,而不是您的观点。