注册/login.html已编辑
{% load i18n %}
<div id="Login">
{% if form.non_field_errors %}
<p class="error">
{% for err in form.non_field_errors %}{{ err }}
{% if not forloop.last %}<br/>{% endif %}
{% endfor %}
</p>
{% endif %}
<form method="post" action=".">{% csrf_token %}
<table>
<tr><td><label for="id_username">{% trans 'USERNAME' %}:</label></td><td>{{ form.username }}</td></tr>
{% if form.username.errors %}<tr><td class="error" colspan="2">***{{ form.username.errors|join:", " }}</td></tr>{% endif %}
<tr><td><label for="id_password">{% trans 'PASSWORD' %}:</label></td><td>{{ form.password }}</td></tr>
{% if form.password.errors %}<tr><td class="error" colspan="2">***{{ form.password.errors|join:", " }}</td></tr>{% endif %}
</table>
<input type="submit" value="{% trans 'sign in' %}" />
{% url registration_register as registration_register %}
{% if registration_register %}
<span><a href="{% url registration_register %}">{% trans "register" %}</a></span>
{% endif %}
<input type="hidden" name="next"
{% if next %}
value={{ next }} />
{% else %}
{% url satchmo_account_info as accounturl %}
{% if accounturl %} value="{% url satchmo_account_info %}" /> {% endif %}
{% endif %}
</form>
{% comment %} We jump through hoops with the urls so it doesn't bomb with django's built in unit tests.{% endcomment %}
{% url auth_password_reset as auth_password_reset %}
{% if auth_password_reset %}
<p>{% trans "If you do not remember your password, please" %}
<a href="{% url auth_password_reset %}">{% trans "click here</a> to have it reset." %}</p>
{% endif %}
</div>为什么不显示form.username和form.password输入框?我必须删除块标记{% extends shop/base.html %}。这会导致输入字段消失吗?
我所做的是删除{% block content %}{% endblock %},并在base.html模板中使用{% include“注册/login.html”%}。我希望登录部分显示在左上角,而不是必须为位于{% block content %}中的登录字段单击" login“。
注册/login.html原始文件
{% extends "shop/base.html" %}
{% load i18n %}
{% block navbar %}
<li class="first"><a href="{{ shop_base }}/">{% trans "Home" %}</a></li>
{% endblock %}
{% block content %}
{% if form.non_field_errors %}
<p class="error">{% for err in form.non_field_errors %}{{ err }}{% if not forloop.last %}<br/>{% endif %}
{% endfor %}</p>
{% endif %}
<form method="post" action=".">{% csrf_token %}
<table>
<tr><td><label for="id_username">{% trans 'Email address' %}:</label></td><td>{{ form.username }}</td></tr>
{% if form.username.errors %}<tr><td class="error" colspan="2">***{{ form.username.errors|join:", " }}</td></tr>{% endif %}
<tr><td><label for="id_password">{% trans 'Password' %}:</label></td><td>{{ form.password }}</td></tr>
{% if form.password.errors %}<tr><td class="error" colspan="2">***{{ form.password.errors|join:", " }}</td></tr>{% endif %}
</table>
<input type="submit" value="{% trans 'Login' %}" />
<input type="hidden" name="next"
{% if next %}
value={{ next }} />
{% else %}
{% url satchmo_account_info as accounturl %}
{% if accounturl %} value="{% url satchmo_account_info %}" /> {% endif %}
{% endif %}
</form>
{% comment %} We jump through hoops with the urls so it doesn't bomb with django's built in unit tests.{% endcomment %}
{% url registration_register as registration_register %}
{% url auth_password_reset as auth_password_reset %}
{% if registration_register %}
<p>{% trans "If you do not have an account, please" %} <a href="{% url registration_register %}">{% trans "click here" %}</a>.</p>
{% endif %}
{% if auth_password_reset %}
<p>{% trans "If you do not remember your password, please" %} <a href="{% url auth_password_reset %}">{% trans "click here</a> to have it reset." %}</p>
{% endif %}
{% endblock %}shop/base.html
<div id="sidebar-primary">{# rightnav #}
{% block sidebar-primary %}
<h3>{% trans "Quick Links" %}</h3>
{% url satchmo_product_recently_added as recenturl %}
{% if recenturl %}<a href="{{ recenturl }}">{% trans "Recently Added" %}</a>{% endif %}
{% url satchmo_product_best_selling as popularurl %}
{% if popularurl %}<br/><a href="{{ popularurl }}">{% trans "Best Sellers" %}</a><br/>{% endif %}
{% url satchmo_category_index as category_index %}
{% if category_index %} <a href="{{ category_index }}">{% trans "Category Index" %}</a><br /> {% endif %}
{% url satchmo_quick_order as quick_order %}
{% if quick_order %}<a href="{{ quick_order }}">{% trans "Quick Order" %}</a> {% endif %}
{% plugin_point "sidebar_links" %}
<h3>{% trans "Account Information" %}</h3>
{% if user.is_staff %}
<a href="{% url admin:index %}" target="blank">{% trans "Admin" %}</a><br/>
{% endif %}
{% if user.is_authenticated %}
{% url satchmo_account_info as accounturl %}
{% if accounturl %}<a href="{{ accounturl }}" target="blank">{% trans "Account Details" %}</a><br/>{% endif %}
<a href="{{ logout_url }}?next={{request.path}}">{% trans "Log out" %}</a><br/>
{% else %}
<!-- I REMOVE REPLACED THE LINK BELOW WITH {% include "registration/login.html" %} -->
<a href="{{ login_url }}?next={{request.path}}">{% trans "Log in" %}</a><br/>
{% endif %}
{% url satchmo_cart as carturl %}
{% if carturl %}<a href="{{ carturl }}">{% trans "Cart" %}</a>{% endif %}
{% if not cart.is_empty %}
({{ cart_count|normalize_decimal }} - {% if sale %}{{ cart|discount_cart_total:sale|currency }}{% else %}{{cart.total|currency}}{% endif%}) <br/>
{% url satchmo_checkout-step1 as checkouturl %}
{% if checkouturl %}<a href="{{ checkouturl }}">{% trans "Check out" %}</a>{% endif %}
{% endif %}
{% plugin_point "shop_sidebar_actions" %}
{% url satchmo_contact as contact_url %}
{% if contact_url %}<p><a href="{{ contact_url }}">{% trans "Contact Us" %}</a></p>{% endif %}
{% satchmo_language_selection_form %}
{% block sidebar-primary-bottom %}
{% plugin_point "shop_sidebar_primary" %}
{% endblock %}
{% endblock sidebar-primary %}
</div>我尝试了一个{% include "registration/copy_login.html" %},对内容做了一些改动。我还使用了<form action="{% url auth_login %}。当我在填写了登录/通行证后单击submit时,它会将我带到/accounts/login/,在这里我必须再次输入登录数据。
这是我的copy_login.html
# copy_login.html
...
<tr><td><label for="id_username">{% trans "Username" %}</label></td><td><input type="text" name="id_username" id="id_username" /></td></tr>
<tr><td><label for="id_password">{% trans "Password" %}</label></td><td><input type="text" name="id_password" id="id_password" /></td></tr>
...发布于 2012-03-15 21:34:26
如果你的模板扩展了另一个模板,那么只有它在{% block %}...{% endblock %}中的代码才会“显示”。
假设我有这个base.html模板:
<html>
<body>
{% block body %}
{% endblock %}
</body>
</html>然后,在这样的login.html模板中:
{% extends 'base.html' %}
this won't show up because it's not in a block
{% block body %}
this will "show up"
{% endblock %}阅读关于template inheritance的文章
发布于 2012-04-19 17:53:34
仅针对用户代码的回答
第一个问题是,您可能试图将修改后的缩短注册/login.html包含到主模板中,但您将其隐藏在注释中:
<!-- I REMOVE REPLACED THE LINK BELOW WITH {% include "registration/login.html" %} -->取消注释以查看结果。
原始模板注册/login.html由视图accounts.views.emaillogin和URL /accounts/login/使用,如果您转到任何需要登录的页面,URL URL都会重定向。你打破了它,但在这种情况下,你想在页面的中心显示一个更大的表单,而不仅仅是角落的小表单。您也不希望在页面上显示与其他表单相关的错误。难到不是么?不要破坏原始模板的用途。
一般答案
我建议首先将登录模板registration/login.html的重要部分复制粘贴到您包含在某个位置的较小模板中。这样你就不会在小模板中包含错误消息等,只包含最小的错误消息。如果登录失败,将显示通常的带有消息的大登录页面。您需要将action="."更改为
<form method="post" action="{% url auth_login %}?next={{ request.path }}">注意:名称auth_login在satchmo_store/accounts/urls.py中定义为:
(r'^login/$', 'emaillogin', {'template_name': 'registration/login.html'}, 'auth_login'),最后,你可以让它变干(不要重复),但这对你来说不值得,因为模板会有很大的不同。
编辑1)包括来自评论的小修复。2)修改后便于他人使用。
https://stackoverflow.com/questions/9710677
复制相似问题