1
url(r"^(\d+)/$", 'cal.views.year', name="year"),
that is what I tried
to do, but I still get an error (in this care my error is: βstr object
is not callable)β¦
Ok, changing βyearβ with the existing view partially solved my
problem. Thank you! Could you please also tell me how to refer to
cal.views.month to display the month? Should βmonthβ be rendered to
response in views?
URL patterns in django have two required arguments:
- The pattern to match.
- The function to call when the pattern in matched.
The second argument must be an existing function (or class) in your views. The function can take multiple arguments, and appear in multiple patterns.
For example, suppose you have a method like this:
def display_events(request, year=None, month=None, day=None):
# do something to filter events
events = Event.objects.all()
if year:
events.filter(event_date__year=year)
if month:
events.filter(event_date__month=month)
if day:
events.filter(event_date__day=day)
return render(request, 'events_list.html', {'events': events})
You want to map it to the following URLs:
- /events/2014
- /events/2014/06
- /events/2014/06/05
Here is how you would set it up:
url(r'^events/(?P<year>\d{4})/(?P<month>\d{2})/(?P<day>\d{2})$',
'cal.vews.display_events', name='daily'),
url(r'^events/(?P<year>\d{4})/(?P<month>\d{2})$',
'cal.views.display_events', name='monthly'),
url(r'^events/(?P<year>\d{4})$',
'cal.views.display_events', name='yearly'),
url(r'^events/$', 'cal.views.display_events', name='all-events'),
The complicated part is the regular expression, a short intro is provided in the documentation:
r'
(this is a Python raw string, which means everything is taken literally and special characters are not interpreted).^
β start of stringevents/
β the literal string events/(
β start of a capturing group. This means, everything that matches the expression until the closing)
will be captured (returned) by the engine.?P<year>
β this means βwhatever is captured in the group, return it the name βyear'β.
These are called named capture groups.
See the Python documentation for more.\d
β A number{4}
β exactly four of the previous pattern)
β close of the capture group$
β end of string'
β end the raw string
The long verbose version of the above is:
Search the url. If you see events/
followed by exactly four numbers, capture these four numbers and send them as the name year
as an argument to the view method with the name display_events
which will be in the views
module of the cal
module.
Applications in django are just Python modules.
Since patterns are applied in the first order they are matched, you should place the most βliberalβ pattern last, so that if nothing else matches, it will match.
All these patterns point to the same view method. Just that depending on what is matched in the URL, the different arguments will be passed.
Finally, keep in mind that URL patterns cannot match query strings (the part after the ?
in a URL). This information is always passed to the view in the dict-like object request.GET
.