简体   繁体   English

将日期传递给网址标签Django

[英]Passing Date to Url Tag Django

I am trying to pass a date to a URL Tag. 我正在尝试将日期传递给URL标记。 Here is my url regex: 这是我的网址正则表达式:

url(r'^fund_details/?startDate=(?P<date_index>\d{2}-\d{2}-\d{4})/?offset(?P<date_offset>\d{2}-\d{2}-\d{4})/

and my url tag: 和我的网址标签:

{% url 'fund_monitor:fund_details' start_date offset %}

But i get this error 但是我得到这个错误

ValueError: invalid literal for int() with base 10: '08-18-2017'

Note my start date is '08-18-2017' . 注意我的开始日期是'08-18-2017' I think it is because my regex is looking for a number rather than a string. 我认为这是因为我的正则表达式正在寻找一个数字而不是一个字符串。 However, I need the - in between the numbers. 但是,我需要在数字之间使用- How can I achieve this? 我该如何实现?

A cleaner way of achieving the intended result would be specifying the URL structure up until the query string, and implement the logic in a view function or class. 实现预期结果的一种更干净的方法是指定URL结构直到查询字符串,然后在视图函数或类中实现逻辑。 Practically speaking, you should have the following: 实际上,您应该具备以下条件:

urls.py (or whatever filename containing the URL bindings): urls.py (或包含URL绑定的任何文件名):

url(r'^fund_details/$', YourViewClass.as_view(), name="url_alias")

(Note the $ symbol in the end - as far as my experience is concerned, that should work even if your full URL has a query string in the end.) (请注意,最后的$符号-就我的经验而言,即使您的完整URL的末尾带有查询字符串,该符号也应该起作用。)

views.py or other arbitrarily-named file: views.py或其他任意命名的文件:

def handle_request(request):
    start_date = request.GET["start_date"]
    date_offset = request.GET["date_offset"]

    # Do your own validation with the acquired input
    # Compute the output with the just validated data

Constructing a query string as ?startDate=01-01-2017&offset=01-01-2016 is easily (or rather, automatically) done using a HTML form. 使用HTML表单轻松(或自动)将查询字符串构造为?startDate=01-01-2017&offset=01-01-2016

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM