gpt4 book ai didi

python - 通过创建可重用模板来最大限度地减少代码重复

转载 作者:行者123 更新时间:2023-12-01 07:02:56 25 4
gpt4 key购买 nike

我有一个模板,其中包含 4 个不同的分页器,它们仅在上下文变量上有所不同(tasks_today、tasks_tomorrow 等),并且我希望最大限度地减少代码重复,因此我没有 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 %}

唯一的缺点是,这只能让您替换网址中的一个查询参数。

关于python - 通过创建可重用模板来最大限度地减少代码重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58560261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com