我希望使用日期筛选器将日期时间类型的值作为参数提供给url。
我的网址一定是这样的:/account/detail-of-cash-flow/2020-8-10
这个命令是:{{item.date_field|date:'Y-m-d'}} = '2020-8-10‘。但是,当我将此命令实现为模板url时,无法工作。
template.html
{% for item in cash_flow_data %}
<tr class='clickable-row' data-href="{% url 'account:detail_of_cash_flow' item.date_field|date:'Y-m-d' %}">
<td>{{ item.date_field }}</td>
<td>{{ item.EUR }}</td>
<td>{{ item.USD }}</td>
<td>{{ item.GBP }}</td>
<td>{{ item.TRY }}</td>
</tr>
{% endfor %}urls.py
app_name = "account"
urlpatterns = [
path('daily-cash-flow/', views.daily_cash_flow, name = "daily_cash_flow"),
path('detail-of-cash-flow/<slug:slug>/', views.detail_of_cash_flow, name = "detail_of_cash_flow")
]我希望我能解释我的问题。
发布于 2020-10-08 09:46:48
在项目模型添加方法中,该方法将返回所需的格式。
class ItemModel(models.Model):
...
def get_url_date(self):
return self.date_field.strftime("%Y-%m-%d")然后在模板中可以使用
<a href="{% url 'account:detail_of_cash_flow' item.get_url_date %}">link</a>upd:根据您的上下文,您有几个变体
context_flow_data =[{ 'url_date':item_data‘date_field’..strftime(“%Y-%m-%d”),'date':item_data'date_field',‘美元’:item_data'USD','EUR':item_data'EUR','GBR':item_data'GBR',} for item_data in cash_flow_data ]
然后将这些数据提供给上下文并在模板中使用。
<a href="{% url 'account:detail_of_cash_flow' item.url_date %}">link</a>第二个变体:您可以添加行
urlpatterns = [
...
path('detail-of-cash-flow/', views.detail_of_cash_flow, name = "detail_of_cash_flow")
...
]然后在模板中使用
<a href="{% url 'account:detail_of_cash_flow' %}{{item.date_field|date:'Y-m-d'}}/">link</a>https://stackoverflow.com/questions/64259691
复制相似问题