1

registration/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 %} にあるログイン フィールドで [ログイン] をクリックするのではなく、ログイン セクションを左上隅に表示する必要がありました。

registration/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

ユーザーのコードのみに固有の回答

最初の問題は、変更された短縮された registration/login.html をメイン テンプレートに含めようとした可能性がありますが、それをコメントに隠していることです。

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

コメントを外して結果を確認します。

元のテンプレート registration/login.html はビューで使用され、ログインが必要なページに移動した場合にリダイレクトaccounts.views.emailloginされる URLで使用されます。/accounts/login/あなたはそれを壊しましたが、この場合、ページの隅にある小さなフォームだけでなく、より大きなフォームをページの中央に表示したいと考えています。また、ページ上の他のフォームに関連するエラーを表示したくありません。ではない?元のテンプレートの目的を壊さないでください。

一般的な答え

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 (Do not Repeat Yourself) にすることができますが、テンプレートは非常に異なるため、努力する価値はありません。

[編集済み] 1) コメントからの小さな修正を含めました。2) 他の方が使いやすいように修正しました。

于 2012-04-19T09:53:34.077 に答える