[英]Assigning a datetime object as the url parameter
I want to give the value of Datetime type to url as parameter using date filter.我想使用日期过滤器将 Datetime 类型的值作为参数提供给 url。
My url must be such: /account/detail-of-cash-flow/2020-8-10
我的 url 一定是这样的: /account/detail-of-cash-flow/2020-8-10
This command: {{item.date_field|date:'Ym-d'}}
= '2020-8-10'.此命令: {{item.date_field|date:'Ym-d'}}
= '2020-8-10'。 But, not working when i this commands implement to template url.但是,当我将此命令实施到模板 url 时,它不起作用。
template.html模板.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网址.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")
]
I hope I was able to explain my problem.我希望我能够解释我的问题。
in your item model add method which will return format you need在您的项目 model 添加方法,它将返回您需要的格式
class ItemModel(models.Model):
...
def get_url_date(self):
return self.date_field.strftime("%Y-%m-%d")
and then in template you can use然后在模板中你可以使用
<a href="{% url 'account:detail_of_cash_flow' item.get_url_date %}">link</a>
upd: according to your context you have several variants更新:根据您的上下文,您有几种变体
context_flow_data = [ { 'url_date': item_data['date_field'].strftime("%Y-%m-%d"), 'date': item_data['date_field'], 'USD': item_data['USD'], 'EUR': item_data['EUR'], 'GBR': item_data['GBR'], } for item_data in cash_flow_data ]
and then provide this data to your context and in template use然后将此数据提供给您的上下文和模板使用
<a href="{% url 'account:detail_of_cash_flow' item.url_date %}">link</a>
second variant: you can add line第二种变体:您可以添加行
urlpatterns = [
...
path('detail-of-cash-flow/', views.detail_of_cash_flow, name = "detail_of_cash_flow")
...
]
and then in template use然后在模板中使用
<a href="{% url 'account:detail_of_cash_flow' %}{{item.date_field|date:'Y-m-d'}}/">link</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.