Вопрос по – Условное назначение переменных в шаблоне Django

13

Error: User Rate Limit Exceeded

{% if username %}
    {% with menu_user=username %}
{% elif recent_users %}
    {% with sorted_users=recent_users|dictsortreversed:"timestamp" %}
    {% with menu_user=sorted_users.0.username %}
{% endif %}
{% if menu_user %}
    <div id="menu">
        <ul>
            <li><a href="/user/{{ menu_user|urlencode }}">Profile</a></li>
            <li><a href="/user/{{ menu_user|urlencode }}/products/">Products</a></li>
        </ul>
    </div>
{% endif %}
{% if recent_users %}
    {% endwith %}
{% endif %}
    {% endwith %}

Error: User Rate Limit Exceeded

if username:
    menu_user = username
elif recent_users:
    menu_user = sorted(recent_users)[0]['username']

if menu_user:
    <div id="menu">
        <ul>
            <li><a href="/user/{{ menu_user|urlencode }}">Profile</a></li>
            <li><a href="/user/{{ menu_user|urlencode }}/products/">Products</a></li>
        </ul>
    </div>
Error: User Rate Limit ExceededviewError: User Rate Limit Exceeded seler
Error: User Rate Limit Exceeded olofom

Ваш Ответ

3   ответа
1

Error: User Rate Limit Exceeded

@register.assignment_tag
def alias(obj):
    """
    Alias Tag
    """
    return obj

Error: User Rate Limit Exceeded

{% alias sorted_users.0.username as menu_user %}
Error: User Rate Limit ExceededwithError: User Rate Limit ExceededifError: User Rate Limit ExceededifError: User Rate Limit Exceeded
Error: User Rate Limit ExceededwithError: User Rate Limit Exceeded
6

updateError: User Rate Limit Exceeded

@register.inclusion_tag('menu_snippet.html')  # or you could use takes_context=True and fetch values from the context
def render_menu(username, recent_users):
    if username:
        menu_user = username
    elif recent_users:
        # sorted here could be replaced by min or QuerySet method, it depends
        # for example: 
        # menu_user = min(recent_users, key=lambda u:u.timestamp).username
        menu_user = sorted(recent_users)[0]['username']
    return {'menu_user':menu_user}

# in template, it looks like
{% render_menu username recent_users %}

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

{% if username %}
    <div id="menu">
        <ul>
            <li><a href="/user/{{ username|urlencode }}">Profile</a></li>
            <li><a href="/user/{{ username|urlencode }}/products/">Products</a></li>
        </ul>
    </div>
{% else %}
    {% if recent_users %}
    {% with sorted_users=recent_users|dictsortreversed:"timestamp" %}
    {% with menu_user=sorted_users.0.username %}
    <div id="menu">
        <ul>
            <li><a href="/user/{{ menu_user|urlencode }}">Profile</a></li>
            <li><a href="/user/{{ menu_user|urlencode }}/products/">Products</a></li>
        </ul>
    </div>
    {% endwith %}{% endwith %}
    {% endif %}
{% endif %}

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded olofom
Error: User Rate Limit Exceeded olofom
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded{% render_menu username recent_users %}Error: User Rate Limit Exceededthe doc
0

Error: User Rate Limit Exceeded

Похожие вопросы