Django NoReverseMatch - 具有两个参数的视图

时间:2014-12-29 14:31:40

标签: python django django-templates reverse django-urls

views.py

def cal_main(request, year=None):
if year: year = int(year)
else:    year = time.localtime()[0]

nowy, nowm = time.localtime()[:2]
lst = []

# create a list of months for each year, indicating ones that contain entries and current

for y in [year, year+1, year+2]:
    mlst = []
    for n, month in enumerate(mnames):
        entry = current = False   # are there Entry(s) for this month; current month?
        entries = Entry.objects.filter(date__year=y, date__month=n+1)
        if y == nowy and n+1 == nowm:
            current = True

        if entries:
            entry = True
        mlst.append(dict(n=n+1, name=month, entry=entry, current=current))
    lst.append((y, mlst))

返回render_to_response(“calend.html”,dict(years = lst,user = request.user,year = year,reminders = reminders(request)))

urls.py

url(r'^(\d+)/$', 'cal_main'),
url(r'^calendar/$','cal_main'),  

calend.html

"{% url 'views.cal_main' year|add:'1' %}"    

我想显示当前和明年但我收到错误:

  

NoReverseMatch反转带有参数“(2015,)”的“views.cal_main”   和找不到关键字参数“{}”。尝试了0种模式:[]

我已经找到了一些如何处理这个问题的技巧,我觉得我尝试了所有的东西,但总有一样的错误。我之前从未使用过django,所以在尝试了人们在互联网上写的内容后,我不知道还能尝试什么,所以如果有人能帮助我,我会很高兴。

我正在使用django 1.7和python 2.7。

1 个答案:

答案 0 :(得分:0)

url模板标记中传递网址名称

"{% url 'cal_main' year|add:'1' %}"