gpt4 book ai didi

Django 没有与参数的反向匹配

转载 作者:行者123 更新时间:2023-12-04 18:09:46 25 4
gpt4 key购买 nike

从我的模板:

<a href="{% url 'tracker:othermonth' year next_month %}">July</a>

网址模式:
url(r'^ocal/$', views.calendar, name = "othermonth"),

看法:
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))

错误:
Reverse for 'othermonth' with arguments '(2013, 7)' and keyword arguments '{}' not found.

我已经搜索过 stackoverflow 和 django 文档,但我似乎无法弄清楚为什么我会得到这个 NoReverseMatch错误。我确信这对我来说是一个非常简单的疏忽,因为我正在盯着以前项目中的代码,该代码几乎与此相同并且工作正常。任何帮助将不胜感激,谢谢。

更新:我尝试删除我试图通过 URL 发送的参数并修复了 NoReverseMatch然而,被调用的 View 需要这些参数,因此链接失败。

最佳答案

您打算如何将这些参数嵌入到您的 URL 中?没有任何东西可以捕获它们,也无法进行反向查找来构造它。您需要接受这些参数的 URL 模式。就像是:

url(r'^ocal/(?P<year>\d{4})/(?P<month>(0|1)?\d)/$', views.calendar, name = "othermonth_bymonth"),

在这里使用关键字而不是位置参数是可选的,但我认为它使事情变得更容易 - 并允许设置可以触发行为的默认值,例如在未指定任何内容的情况下显示当天的日历。

另外,您的 mytickets查询集也可以这样构造:
mytickets = Event.objects.filter(on_sale__year=year, onsale__month=month)

我认为阅读起来更清晰。

实际上,仔细观察一下 - Django 内置的基于日期的 View 可以为您做很多事情。如果您还没有研究过它们,我建议您这样做:

https://docs.djangoproject.com/en/dev/ref/class-based-views/generic-date-based/

对于这个特定的 View ,您需要做的就是子类 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>

显然,您可以在这里使用年和日 View 做更多的事情,但这应该展示一般概念。

关于Django 没有与参数的反向匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17202861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com