問題

我有一個模板,其中包含4個不同的分頁器,它只在上下文變數(tasks_ther,tasks_tork,...等)中有所不同,我想盡量減少程式碼重複,所以我沒有4個不同的分頁器模板.

模板:

 <div class="wrapper">
    <h3>Today</h3>
    <table>
    {% if tasks_today %}
        {% for task in tasks_today %}
            {% include 'todo/task_table_row.html' %}
        {% endfor %}
        {% include 'todo/paginator_today.html' %}
    {% else %}
        <p>No tasks for today.</p>
    {% endif %}
    </table>
    <h3>Tomorrow</h3>
    <table>
    {% if tasks_tomorrow %}
        {% for task in tasks_tomorrow %}
            {% include 'todo/task_table_row.html' %}
        {% endfor %}
        {% include 'todo/paginator_tomorrow.html' %}
    {% else %}
        <p>No tasks for tomorrow.</p>
    {% endif %}
    </table>
    <h3>Upcoming</h3>
    <table>
    {% if tasks_upcoming %}
        {% for task in tasks_upcoming %}
            {% include 'todo/task_table_row.html' %}
        {% endfor %}
        {% include 'todo/paginator_upcoming.html' %}
    {% else %}
        <p>No upcoming tasks.</p>
    {% endif %}
    </table>
    <h3>Past</h3>
    <table>
    {% if tasks_past %}
        {% for task in tasks_past %}
            {% include 'todo/task_table_row.html' %}
        {% endfor %}
        {% include 'todo/paginator_past.html' %}
    {% else %}
        <p>No tasks in the past.</p>
    {% endif %}
    </table>
</div>
 

paginator_today:

 {% load url_replace %}

{% if tasks_today %}
<div class='paginator'>
    <nav aria-label="Page navigation">
        <ul class="pagination">
            {% if tasks_today.has_previous %}
                <li class="page-item">
                    <a class="page-link" href="?{% url_replace page_today=1 %}" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
                <span class="sr-only">begin</span>
            </a>
                </li>
            {% endif %}

    {% for n in tasks_today.paginator.page_range %}
        {% if tasks_today.number == n %}
        <li class="page-item active">
            <span class="page-link">{{ n }}
                <span class="sr-only">(current)</span>
            </span>
        </li>
        {% elif n > tasks_today.number|add:'-3' and n < tasks_today.number|add:'3' %}
            <li class="page-item">
                <a class="page-link" href="?{% url_replace page_today=n %}">{{ n }}</a>
            </li>
        {% endif %}
    {% endfor %}

    {% if tasks_today.has_next %}
        <li class="page-item">
            <a class="page-link" href="?{% url_replace page_today=tasks_today.paginator.num_pages %}" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
                <span class="sr-only">end</span>
            </a>
        </li>
    {% endif %}
        </ul>
    </nav>
</div>
{% endif %}
 

paginator_tomorrow:

 {% load url_replace %}

{% if tasks_tomorrow %}
<div class='paginator'>
    <nav aria-label="Page navigation">
        <ul class="pagination">
            {% if tasks_tomorrow.has_previous %}
                <li class="page-item">
                    <a class="page-link" href="?{% url_replace page_tomorrow=1 %}" aria-label="Previous">
                <span aria-hidden="true">&laquo;</span>
                <span class="sr-only">begin</span>
            </a>
                </li>
            {% endif %}

    {% for n in tasks_tomorrow.paginator.page_range %}
        {% if tasks_tomorrow.number == n %}
        <li class="page-item active">
            <span class="page-link">{{ n }}
                <span class="sr-only">(current)</span>
            </span>
        </li>
        {% elif n > tasks_tomorrow.number|add:'-3' and n < tasks_tomorrow.number|add:'3' %}
            <li class="page-item">
                <a class="page-link" href="?{% url_replace page_tomorrow=n %}">{{ n }}</a>
            </li>
        {% endif %}
    {% endfor %}

    {% if tasks_tomorrow.has_next %}
        <li class="page-item">
            <a class="page-link" href="?{% url_replace page_tomorrow=tasks_tomorrow.paginator.num_pages %}" aria-label="Next">
                <span aria-hidden="true">&raquo;</span>
                <span class="sr-only">end</span>
            </a>
        </li>
    {% endif %}
        </ul>
    </nav>
</div>
{% endif %}
 

我能做什麼?

url_replace.py:

 from django.utils.http import urlencode
from django import template
​
register = template.Library()
​
@register.simple_tag(takes_context=True)
def url_replace(context, **kwargs):
    query = context['request'].GET.dict()
    query.update(kwargs)
    return urlencode(query)
 

  最佳答案

您可以使用with關鍵字在包含模板中設定某些變數:

 {% include 'todo/paginator.html' with tasks=tasks_today page='page_today' %}
 

然後您可以將頁面模板中對 tasks_today 的引用替換為 tasks,以及對 page_today 的引用替換為 page


編輯:對url_replace的呼叫是有問題的,因為它使用kwargs,像{% url_replace page_today = x %}這樣的呼叫將等同於對urlreplace(context, **{'page_today' : x})的呼叫.

簡單地用page = x呼叫url_replace將無法正常工作,因為它將以page的名稱傳遞一個關鍵字引數,而不是page的值

天真的解決方案是將page變數傳遞給paginator.html並呼叫{% urlreplace **{page : 20} %},但Django模板引擎不允許包裝kwargs.

我認為最好的方法是建立一個url_replace_single函式,因為在這種情況下,您只替換一個值. 為此,您需要在url_replace函式上定義欄位和值引數:

 @register.simple_tag(takes_context=True)
def url_replace(context, field, value, **kwargs):
    query = context['request'].GET.dict()
    query_dict = { field : value}
    query.update(query_dict)
    return urlencode(query)
 

然後從模板呼叫函式,如下所示:

 {% url_replace field=page value=x %}
 

唯一的缺點是,這隻會讓您替換 url 中的一個查詢引數。

  相同標籤的其他問題

pythondjango