我最近试图回到 Django,但在尝试制作登录页面进行练习时遇到了一些问题。我收到错误“登录只需要 1 个参数(给定 2 个)”,我不知道为什么。这是我的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))
这是我的 login.html 模板:
<!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>
谢谢你的帮助。