我正在尝试以yyyy / mm / dd格式传递日期。它接受最多只有yyyy / mm的月份。当我通过日期时,它表示找不到页面(404)。类似于url weeklyreports / 2011/03/22。
这是我的url.py
url(r'^weeklyreports/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'weeklyreports'),
我也试过了下面的网址
url(r'^weeklyreports/\d{4}/\d{02}/\d{04}/$', 'weeklyreports'),
我的观点weeklyreports.py
def weeklyreports(request, year = None, month = None, day = None):
today = datetime.date.today()
if year:
year = int(year)
month = int(month)
day = int(day)
today = datetime.date(year, month, day)
weekday = today.weekday()
start_delta = datetime.timedelta(days = weekday)
start_of_week = today - start_delta
week_dates = [start_of_week + datetime.timedelta(days=i) for i in range(7)]
previous_week = start_of_week - datetime.timedelta(7)
next_week = start_of_week + datetime.timedelta(7)
return render_to_response('template/weeklyreports.html', locals(),
context_instance = RequestContext(request))
这是我的模板
enter code here
<a href="/myapp/weeklyreports/{{previous_week|date:"Y/m/d"}}"><img src="{{MEDIA_URL}}/img/previous.png"></a>
<b>Weekly Reports</b>
<a href="/myapp/weeklyreports/{{next_week|date:"Y/m/d"}}"><img src="{{MEDIA_URL}}/img/next.png"></a>
这段代码有什么问题?提前致谢
答案 0 :(得分:4)
为您的网址添加名称:
url(r'^weeklyreports/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'weeklyreports', name='weeklyreports'),
在您的模板中,使用您的命名网址weeklyreports
:
<a href="{% url weeklyreports 2011 03 22 %}">
以上是否适用于您?
答案 1 :(得分:0)
仅接受月份即可 YYYY / MM
这些链接:
<a href="/myapp/weeklyreports/{{previous_week|date:"Y/m/d"}}">
<a href="/myapp/weeklyreports/{{next_week|date:"Y/m/d"}}">
应该是:
<a href="/myapp/weeklyreports/{{previous_week|date:"Y/m/d"}}/">
<a href="/myapp/weeklyreports/{{next_week|date:"Y/m/d"}}/">
注意尾部斜杠
URLconf搜索的内容
URLconf搜索 请求的URL,作为普通的Python 串。这不包括GET或 POST参数或域名。
例如,在请求中 http://www.example.com/myapp/, URLconf将查找myapp /.
在请求中 http://www.example.com/myapp/?page=3, URLconf将查找myapp /.
URLconf没有查看 请求方法。换句话说,全部 请求方法 - POST,GET,HEAD, 等 - 将被路由到同一个 相同网址的功能。
http://docs.djangoproject.com/en/dev/topics/http/urls/#example