django - 如何渲染我在网页中随处使用的模板?
我正在使用 django 1.2.4。 我在 Registration/login.html 中有一个登录模板(操作是 django.contrib.auth.views.login),我想将其包含在每个页面上。我在 base.html 上创建了一个块,就像为每个模板所做的那样。问题是浏览器无法识别这个登录块,我认为这是因为我只为每个视图渲染一个模板,我没有渲染这个登录模板。
这是我的文件夹结构:
/templates/
base.html
/myapp/
object_list.html
...
/registration/
login.html
...这是我的login.html:
{% extends "base.html" %}
{% block mylogin %}
<div class="horizontal">
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form action="{% url django.contrib.auth.views.login %}" method="post">
{% csrf_token %}
<div class="login_box">
<div class="login_text">{{ form.username.label_tag }}</div><div class="login_input">{{ form.username }}</div>
<div class="password_text">{{ form.password.label_tag }}</div><div class="password_input">{{ form.password }}</div>
<input id="button_login" type="submit" value="" />
</div>
</form>
</div>
{% endblock %}
...在我的base.html 中:
<div id="some_div">
{% block mylogin %} {% endblock %}
</div>
我有一个basestyle.css 包含在base.html 中,其他模板也正确继承...这似乎是一个块问题...
那么..我怎样才能为每个视图渲染这个模板?
谢谢
I'm using django 1.2.4.
I have a template for login in registration/login.html (wich action is django.contrib.auth.views.login) and I want to include it on everypage. I created a block on my base.html as I do for every template. The thing is the browser doesn't recognize this login block and I think it is because I only render a template for each view, I am not rendering this login template.
Here is my folder structure:
/templates/
base.html
/myapp/
object_list.html
...
/registration/
login.html
...and here is my login.html:
{% extends "base.html" %}
{% block mylogin %}
<div class="horizontal">
{% if form.errors %}
<p>Your username and password didn't match. Please try again.</p>
{% endif %}
<form action="{% url django.contrib.auth.views.login %}" method="post">
{% csrf_token %}
<div class="login_box">
<div class="login_text">{{ form.username.label_tag }}</div><div class="login_input">{{ form.username }}</div>
<div class="password_text">{{ form.password.label_tag }}</div><div class="password_input">{{ form.password }}</div>
<input id="button_login" type="submit" value="" />
</div>
</form>
</div>
{% endblock %}
...and in my base.html I have:
<div id="some_div">
{% block mylogin %} {% endblock %}
</div>
I have a basestyle.css included in base.html and the other templates inherit correctly too... it seems to be a block problem...
So.. how can I render this template for every view??
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您正在寻找 include< /a> 标签。这样你就可以在你的 base.html 中包含你的登录 html 片段:
I think you are looking for the include tag. This way you can include your login html snippet in your base.html :
我真正需要的是在 templatetags/ 中创建一个 templatetag mytags.py,我在其中定义了一个名为 get_login 的函数,如下所示:
...在 base.html 中:
现在的问题是模板 (registration/login.html) 无法识别 '{{ formLogin.username }}'、'{{ formLogin.password }}' 等等。
我缺少什么?
更新1:
mark_safe 返回 django.utils.safestring.SafeString 的实例,而不是表单。
使用 (AuthenticationForm() 而不是 mark_safe(AuthenticationForm()) ,它可以工作!
What I really need was to create a templatetag in templatetags/mytags.py, where I define a function called get_login wich looks like this:
...and in base.html:
The problem now is that the template (registration/login.html) doesnt recognize '{{ formLogin.username }}','{{ formLogin.password }}' and so on.
What am I missing?
Update 1:
mark_safe returns an instance of django.utils.safestring.SafeString, not a form.
Use (AuthenticationForm() instead of mark_safe(AuthenticationForm()) and it works!