Django的NoReverseMatch错误

时间:2015-02-23 19:29:27

标签: python django

继续收到此错误:/student/calendar.html上的NoReverseMatch反向' view_event'参数'()'和关键字参数' {u' event_title':u'测试'}'未找到。尝试了1种模式:['学生/日历/活动/ $']

以下是密钥文件的相关位:

views.py:

def calendar_view(request):
   context = RequestContext(request)
   events = Event.objects.all()
   context_dict = {'monday': [], 'tuesday': [], 'wednesday': [], 'thursday': [], 'friday': [], 'saturday': [], 'sunday': []}
   for event in events:
        weekday = event.date.lower()
        if weekday in context_dict:
            context_dict[weekday].append(event)
    return render_to_response('polls/calendar.html', context_dict, context)

def view_event(request, event_title):
    event = Event.objects.filter(title = event_title)
    return render(request, 'polls/detail.html')

urls.py:

url(r'^student/calendar', views.calendar_view, name='calendar'),
url(r'^student/calendar/events/$', views.view_event, name = 'view_event'),

calendar.html:

{% load tags %}


<table border="1">
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
<th>Sunday</th>
</tr>
<tr>
<td>{%for event in tuesday%}
    <a href="{% url 'polls:view_event' event_title=event.title %}">{{ event.title }}, {{event.start_time}} - {{event.end_time}}</a></li> <br>
    {%endfor%} </td>
</tr>

detail.html:

   {{ event.title }}

我的想法是,我有一个事件数据库,并且对于一周中的每一天,我都将它们放在日历上。这部分有效。现在我想添加点击任何事件的可能性来打开一个包含详细信息的新页面(现在它只是标题)。我按照一些例子,但无法解决这个错误...

1 个答案:

答案 0 :(得分:1)

您的view_event视图的网址不带任何参数。你需要这样的东西:

url(r'^student/calendar/events/(?P<event_title>\w+)/$', views.view_event, name = 'view_event'),

请注意,标题不适合在URL中使用,因为它可能包含空格。你应该使用slug或数字ID。

另请注意,您的event_title视图应使用Event.objects.get而不是filter