Python 登录Django 1.5
我在Django 1.5中使用MyUser模型,并使用电子邮件登录: 型号: 我尝试了django.contrib.auth.views导入登录的方法: 网址: 观点:没什么 它适用于经典用户模型中的超级用户,而不是MyUser 然后我试着: 视图: 模板:Python 登录Django 1.5,python,django,email,login,Python,Django,Email,Login,我在Django 1.5中使用MyUser模型,并使用电子邮件登录: 型号: 我尝试了django.contrib.auth.views导入登录的方法: 网址: 观点:没什么 它适用于经典用户模型中的超级用户,而不是MyUser 然后我试着: 视图: 模板: {% if form.errors %} <p>Something is wrong</p> {% endif %} <form action="" method="post"> {% csrf
{% if form.errors %}
<p>Something is wrong</p>
{% endif %}
<form action="" method="post">
{% csrf_token %}
<label for="email">Login:</label>
<input type="text" name="email" value="" id="email"/>
<label for="password">Password:</label>
<input type="password" name="password" value="" id="username">
<input type="submit" value="login" />
<input type="hidden" name="next" value="{{next|escape}}" />
</form>
但是我得到了一个错误login\u view()得到了一个意外的关键字参数“template\u name”
我做错了什么?login\u view()得到了一个意外的关键字参数“template\u name”
意味着您的视图函数应该有template\u name
参数:
def login_view(request, template_name):
'your code'
如果不需要它,请不要在URL.py中传递:
(r'^login/$', login_view),
Upd
您的login\u视图
处理您的POST方法。您可以通过这种方式重新编写它,以便在GET上呈现表单
def login_view(request):
if request.method == 'POST':
username = request.POST['email']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None and user.is_active:
login(request, user)
return HttpResponseRedirect("/n1.html")
return HttpResponseRedirect("/account/invalid/")
form = LoginForm()
return render(request, 'enter.html', {'login_form': LoginForm})
为什么要在
urls.py
中传递模板名称,以及在何处使用它?但如何获取我的enter.html模板?@Wolter请查看此处定义的login\u view
函数。如果你在urls.py
中传递template\u name
,你可以在这里定义的函数中获得它。我不明白为什么从django.contrib.auth.views导入登录(r'^login/$,login,{'template\u name':'enter.html'})工作,但是从视图导入登录(r'^login/$,login\u视图,{'template\u name':'enter.html'}),不工作?我的意思是登录错误\u view()得到一个意外的关键字参数“template\u name”。在哪里可以导入LoginForm()?谢谢!比较一下django。不强制使用表单,但强烈建议使用。表格在django文档中解释。
(r'^login/$', login_view, {'template_name': 'enter.html'}),
def login_view(request, template_name):
'your code'
(r'^login/$', login_view),
def login_view(request):
if request.method == 'POST':
username = request.POST['email']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None and user.is_active:
login(request, user)
return HttpResponseRedirect("/n1.html")
return HttpResponseRedirect("/account/invalid/")
form = LoginForm()
return render(request, 'enter.html', {'login_form': LoginForm})