在base.html中包括Django登录表单 [英] Include Django login form in base.html
问题描述
我是Django的新手,我希望将登录表单包含在基本html中.
我有以下内容:
"registration/login.html"
{%,如果form.has_errors%}< p>您的用户名和密码不匹配.</p> ;,请重试.{% 万一 %}< form method ="post" action =.">{%csrf_token%}< p><标签>用户名:</label>{{form.username}}</p>< p><标签>密码:</label>{{form.password}}</p><输入type ="submit" value =登录"/><input type="hidden" name="next" value="/"/></form>
urls.py
url(r'^ login/$','django.contrib.auth.views.login'),
我在base.html中包括以下内容:
{%包括"registration/login.html"%}
它将呈现除用户名和密码文本框以外的所有内容.我想我缺少什么.
好的,我认为问题是您期望魔术".
Django的观点是模板非常愚蠢.它们仅具有通过以下两种方式之一传递给他们的可用变量:
- 通过context_processor(请参阅Aidan的答案)
- 在调用render(或render_to_response)时传递
在您访问 /login
时呈现表单的原因,但在其他情况下,是因为您使用的 Django 视图 django.contrib.auth.views.login
,将Django AuthenticationForm传递给您的模板.
在此处查看源代码: https://github.com/django/django/blob/master/django/contrib/auth/views.py
没有其他视图神奇地"获得该变量集.
所以您必须:
- 在整个应用的每个视图中自行设置
- 使用上下文处理器.
这是一个简单的 context_processors.py
,它使用Django的东西(基于Aidan的东西):
def include_login_form(request):从django.contrib.auth.forms导入AuthenticationForm形式= AuthenticationForm()返回{'login_form':form}
然后,按照艾丹指出的那样做:
TEMPLATE_CONTEXT_PROCESSORS =(#....'yourapp.context_processors.include_login_form')
现在,问题在于,当您仅包含模板时,登录表单位于名为"login_form"的变量中,但是当django的内置视图使用模板时,登录表单位于名为"form"的变量中./p>
尽管我确信有人可以解决这个问题,但是如果没有好的干净"解决方案,那将是一场磨难.
也许检查在context_processor中传递的上下文,如果已经存在一个名为"form"的变量,请查看它是否是AuthenticationForm的实例,如果是,则使用它来代替.
I am new to Django and I would like to include my login form in base html.
I have the following:
"registration/login.html"
{% if form.has_errors %}
<p>Your username and password didn't match.
Please try again.</p>
{% endif %}
<form method="post" action=".">
{% csrf_token %}
<p><label>Username:</label>
{{ form.username }}</p>
<p><label>Password:</label>
{{ form.password }}</p>
<input type="submit" value="login" />
<input type="hidden" name="next" value="/" />
</form>
urls.py
url(r'^login/$', 'django.contrib.auth.views.login'),
And i included in base.html the following:
{% include "registration/login.html" %}
It renders everything except the textboxes for the username and password. I think I am missing something.
OK, the the problem, I think, is you're expecting "magic".
Django's views are templates are pretty dumb. They only have the variables available to them that are passed in one of two ways:
- Through a context_processor (see Aidan's answer)
- Passed in when calling render (or render_to_response)
The reason the form renders when you visit /login
, but in no other case, is because the Django view you're using django.contrib.auth.views.login
, passed the Django AuthenticationForm into your template for you.
Review the source here: https://github.com/django/django/blob/master/django/contrib/auth/views.py
No other view "magically" gets that variable set.
So you have to either:
- Set it yourself in every view across your whole app
- Use a context processor.
Here's an easy context_processors.py
which uses the Django stuff (based on Aidan's):
def include_login_form(request):
from django.contrib.auth.forms import AuthenticationForm
form = AuthenticationForm()
return {'login_form': form}
Then, do as Aidan points out:
TEMPLATE_CONTEXT_PROCESSORS = (
#....
'yourapp.context_processors.include_login_form'
)
Now, the problem is that, when you just include the template, the login form is in a variable called "login_form", but when django's built-in view uses your template, it's in a variable called "form".
So that's an ordeal without a good "clean" solution, though I'm sure one could sort it out.
Maybe check the context passed in in your context_processor, and if a variable already exists called "form", see if it's an instance of AuthenticationForm, and if it is, use it instead.
这篇关于在base.html中包括Django登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!