apa*_*des 5 django url reverse arguments
从我的模板:
<a href="{% url 'tracker:othermonth' year next_month %}">July</a>
Run Code Online (Sandbox Code Playgroud)
网址格式:
url(r'^ocal/$', views.calendar, name = "othermonth"),
Run Code Online (Sandbox Code Playgroud)
视图:
def calendar(request, year, month):
my_year = int(year)
my_month = int(month)
my_calendar_from_month = datetime(my_year, my_month, 1)
my_calendar_to_month = datetime(my_year, my_month, monthrange(my_year, my_month)[1])
my_tickets = Event.objects.filter(on_sale__gte=my_calendar_from_month).filter(on_sale__lte=my_calendar_to_month)
my_previous_year = my_year
my_previous_month = my_month - 1
if my_previous_month == 0:
my_previous_year = my_year - 1
my_previous_month = 12
my_next_year = my_year
my_next_month = my_month + 1
if my_next_month == 13:
my_next_year = my_year + 1
my_next_month = 1
my_year_after_this = my_year + 1
my_year_before_this = my_year - 1
cal = TicketCalendar(my_tickets).formatmonth(year, month)
return render_to_response('calendar.html', {'events_list': my_tickets,
'calendar': mark_safe(cal),
'month': my_month,
'month_name': named_month(my_month),
'year': my_year,
'previous_month': my_previous_month,
'previous_month_name': named_month(my_previous_month),
'previous_year': my_previous_year,
'next_month': my_next_month,
'next_month_name': named_month(my_next_month),
'next_year': my_next_year,
'year_before_this': my_year_before_this,
'year_after_this': my_year_after_this,
}, context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)
错误:
Reverse for 'othermonth' with arguments '(2013, 7)' and keyword arguments '{}' not found.
Run Code Online (Sandbox Code Playgroud)
我搜索了stackoverflow和django文档,但我似乎无法弄清楚为什么我收到此NoReverseMatch错误.我确信这对我来说是一个非常简单的疏忽,因为我正在盯着前一个项目的代码,这个代码与此几乎相同,并且工作正常.任何帮助将不胜感激,谢谢.
更新:我尝试删除我尝试使用URL发送的参数,并修复了NoReverseMatch但是调用的视图需要这些参数,因此链接失败.
您如何计划将这些参数嵌入到您的URL中?什么都没有捕获它们,并且没有办法进行反向查找来构建它.您需要一个接受这些参数的URL模式.就像是:
url(r'^ocal/(?P<year>\d{4})/(?P<month>(0|1)?\d)/$', views.calendar, name = "othermonth_bymonth"),
Run Code Online (Sandbox Code Playgroud)
在这里使用关键字而不是位置参数是可选的,但我认为它使事情变得更容易 - 并且允许设置可以触发行为的默认值,例如在没有指定任何内容时显示当天的日历.
此外,您的查询集mytickets也可以这样构造:
mytickets = Event.objects.filter(on_sale__year=year, onsale__month=month)
Run Code Online (Sandbox Code Playgroud)
我认为阅读起来有点清洁.
实际上,仔细看看 - Django内置的基于日期的视图可以为你做很多事情.如果你还没有调查过它们,我建议这样做:
https://docs.djangoproject.com/en/dev/ref/class-based-views/generic-date-based/
对于此特定视图,您需要做的就是MonthArchiveView创建TicketCalendar实例并将其添加到上下文中的子类.
编辑:好的,你仍然遇到问题.这就是我要解决这个问题的方法:
views.py
class TicketMonthArchiveView(MonthArchiveView):
allow_empty = True # show months even in which no tickets exist
allow_future = True # show future months
model = Event
def get_context_data(self, **kwargs):
base_context = super(TicketMonthArchiveView, self).get_context_data(**kwargs)
my_tickets = kwargs['object_list']
base_context['calendar'] = mark_safe(TicketCalendar(my_tickets).formatmonth(self.get_year(), self.get_month()))
# the above could be a little off because I don't know exactly how your formatmonth method works
return base_context
urls.py
from somewhere.views import TicketMonthArchiveView
# other patterns, plus:
url(r'^ocal/(?P<year>\d{4})/(?P<month>(0|1)?\d)/$', TicketMonthArchiveView.as_view(), name='calendar_by_month'),
template event_archive_month.html
<a href="{% url 'tracker:calendar_by_month' next_month|date:'%Y' next_month|date:'%m' %}">{{ next_month|date:'%f' }}</a>
Run Code Online (Sandbox Code Playgroud)
显然,你可以在这里看到更多年岁和日视图,但这应该展示一般概念.
| 归档时间: |
|
| 查看次数: |
4722 次 |
| 最近记录: |