python - NoReverseMatch error - simple calendar app -
i cannot figure out how solve problem.
i have tried create simple calendar using lightbird tutorial. got next error:
reverse 'cal.views.main' arguments '(2011,)' , keyword arguments '{}' not found. 1 pattern(s) tried: ['$']
and reason why error occurs:
<a href="{% url "cal.views.main" year|add:'-3' %}"><< prev</a>
my cal/views.py file contains:
def main(request, year=none): ... homecoming render_to_response("cal/main.html", dict(years=lst, user=request.user, year=year,))
my main.html file contains:
{% extends "cal/base.html" %} {% load url future %} <!-- ... --> {% block title %} cal {% endblock %} {% block content %} <a href="{% url "cal.views.main" year|add:'-3' %}"><< prev</a> {% endblock %}
my ulrs.py file contains:
url(r'^admin/', include(admin.site.urls)), url(r'^$', 'cal.views.main', name="main"),
lastly, whole tree consisting of project files looks (i have listed of import files):
website - cal - - models.py - - views.py - website - - static - - templates - - - cal - - - - base.html - - - - main.html - - urls.py
url(r"^(\d+)/$", 'cal.views.year', name="year"),
tried do, still error (in care error is: 'str object not callable)
...
ok, changing "year" existing view partially solved problem. give thanks you! please tell me how refer cal.views.month display month? should "month" rendered response in views?
url patterns in django have 2 required arguments:
the pattern match. the function phone call when pattern in matched.the sec argument must existing function (or class) in views. function can take multiple arguments, , appear in multiple patterns.
for example, suppose have method this:
def display_events(request, year=none, month=none, day=none): # 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) homecoming render(request, 'events_list.html', {'events': events})
you want map next urls:
/events/2014 /events/2014/06 /events/2014/06/05here how set 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 regular expression, short intro provided in documentation:
r'
(this python raw string, means taken literally , special characters not interpreted). ^
- start of string events/
- literal string events/ (
- start of capturing group. means, matches look until closing )
captured (returned) engine. ?p<year>
- means "whatever captured in group, homecoming name 'year'". these called named capture groups. see python documentation more. \d
- number {4}
- 4 of previous pattern )
- close of capture group $
- end of string '
- end raw string the long verbose version of above is:
search url. if see events/
followed 4 numbers, capture these 4 numbers , send them name year
argument view method name display_events
in views
module of cal
module.
applications in django python modules.
since patterns applied in first order matched, should place "liberal" pattern last, if nil else matches, match.
all these patterns point same view method. depending on matched in url, different arguments passed.
finally, maintain in mind url patterns cannot match query strings (the part after ?
in url). info passed view in dict-like object request.get
.
python django url views
No comments:
Post a Comment