1

注册/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 "registration/login.html" %}。我希望登录部分出现在左上角,而不必为位于 {% block content %} 中的登录字段单击“登录”。

注册/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 %}

商店/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 %}。当我点击提交并填写登录名/密码时,它会将我带到/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>
 ...
4

2 回答 2

0

如果您的模板扩展了另一个模板,那么只有它在 {% 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 %}

阅读有关模板继承的信息

于 2012-03-15T13:34:26.390 回答
0

仅针对用户代码回答

第一个问题是您可能尝试将修改后的缩短注册/login.html 包含到主模板中,但您将其隐藏在注释中:

<!-- I REMOVE REPLACED THE LINK BELOW WITH {% include "registration/login.html" %} -->

取消注释以查看结果。

原始模板registration/login.html 由视图accounts.views.emaillogin使用并由URL 使用,/accounts/login/如果您转到任何需要登录的页面,该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'),

最后,您可以将其设为 DRY(不要重复自己),但这对您来说不值得,因为模板会非常不同。

[已编辑] 1) 包括评论中的小修复。2)修改为对其他人更容易。

于 2012-04-19T09:53:34.077 回答