开发者

Unable to pass date in django URL

I am trying to pass date in yyyy/mm/dd format. Its accepting up to month only that is yyyy/mm. When I pass date it says Page not found (404). something like in url weeklyreports/2011/03/22.

Here is my url.py

url(r'^weeklyreports/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'weeklyreports'),

I have tried the be开发者_如何学Golow url also

url(r'^weeklyreports/\d{4}/\d{02}/\d{04}/$', 'weeklyreports'), 

My view 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))

Here is my Template

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>

Whats wrong in this code? Thanks in advance


Add a name to your url:

url(r'^weeklyreports/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d+)/$', 'weeklyreports', name='weeklyreports'),

In your template, use your named url weeklyreports:

<a href="{% url weeklyreports 2011 03 22 %}">

Does the above work for you?


Its accepting up to month only that is yyyy/mm

these links:

<a href="/myapp/weeklyreports/{{previous_week|date:"Y/m/d"}}">
<a href="/myapp/weeklyreports/{{next_week|date:"Y/m/d"}}"> 

should be:

<a href="/myapp/weeklyreports/{{previous_week|date:"Y/m/d"}}/">
<a href="/myapp/weeklyreports/{{next_week|date:"Y/m/d"}}/">

Notice the trailing slash

What the URLconf searches against

The URLconf searches against the requested URL, as a normal Python string. This does not include GET or POST parameters, or the domain name.

For example, in a request to http://www.example.com/myapp/, the URLconf will look for myapp/.

In a request to http://www.example.com/myapp/?page=3, the URLconf will look for myapp/.

The URLconf doesn't look at the request method. In other words, all request methods -- POST, GET, HEAD, etc. -- will be routed to the same function for the same URL.

http://docs.djangoproject.com/en/dev/topics/http/urls/#example

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜