【问题标题】:Django NoReverseMatch - view with two parametersDjango NoReverseMatch - 使用两个参数查看
【发布时间】:2015-02-25 18:03:11
【问题描述】:

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))

return 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。

【问题讨论】:

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


    【解决方案1】:

    url模板标签中传递url名称

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

    【讨论】:

    • 它似乎有效。非常感谢,我永远不会这样做:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 2020-10-14
    • 2017-05-14
    • 2013-06-12
    • 1970-01-01
    • 2022-08-09
    相关资源
    最近更新 更多