Python 将datetime对象指定为url参数
我想使用date filter将Datetime type的值作为参数提供给url 我的url必须是这样的:Python 将datetime对象指定为url参数,python,python-3.x,django,django-templates,Python,Python 3.x,Django,Django Templates,我想使用date filter将Datetime type的值作为参数提供给url 我的url必须是这样的:/account/detailofcash flow/2020-8-10 此命令:{{item.date_field}日期:'Y-m-d'}='2020-8-10'。但是,当我将此命令实现为模板url时,它不起作用 template.html {% for item in cash_flow_data %} <tr class='clickable-row' data-hr
/account/detailofcash flow/2020-8-10
此命令:{{item.date_field}日期:'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 %}
{现金流数据%中项目的%
{{item.date_field}
{{item.EUR}
{{item.USD}}
{{item.GBP}
{{item.TRY}
{%endfor%}
url.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")
]
app\u name=“账户”
URL模式=[
路径('daily-cash-flow/',views.daily\u cash\u flow,name=“daily\u cash\u flow”),
路径(“现金流详情/”,views.detail\u of现金流,name=“现金流详情”)
]
我希望我能够解释我的问题。在您的项目模型中添加方法,该方法将返回您需要的格式
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>
然后将这些数据提供给您的上下文
以及在模板使用中
<a href="{% url 'account:detail_of_cash_flow' item.url_date %}">link</a>
<a href="{% url 'account:detail_of_cash_flow' %}{{item.date_field|date:'Y-m-d'}}/">link</a>
然后在模板中使用
<a href="{% url 'account:detail_of_cash_flow' item.url_date %}">link</a>
<a href="{% url 'account:detail_of_cash_flow' %}{{item.date_field|date:'Y-m-d'}}/">link</a>
这个建议不起作用。我得到:与“现金流的详细信息”相反,未找到参数“(”)”。尝试了1种模式:['account/detail\\\\\\\\\\-of\\\-cash\\\-flow/(?P[-a-zA-Z0-9\+)/$']{{item.get\u url\u date}}
=null如果date\u字段为null,您想要什么url?Ohh-sory。记住现在。我的上下文数据: