2015-02-23 12 views
0
NoReverseMatch错误

请收到此错误:NoReverseMatch在/student/calendar.html反向的 'view_event' 与参数 '()' 和关键字参数 '{u'event_title':u'Test '}' 未找到。 1种模式(S)尝试: '学生/日历/事件/ $']使用Django

这里是密钥文件中的相关内容:

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

您的view_event视图URL不带任何参数。你需要有这样的事情:

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

注意,标题是不是真的适合用在URL无论如何,它大概可以包含空格。你应该使用一个slug或一个数字ID。

另外请注意,您的看法EVENT_TITLE应该使用Event.objects.getfilter

+0

谢谢!有没有办法在创建事件时自动创建一个slug/id? 此外,增加你的行现在会导致这个错误:NoReverseMatch在/student/calendar.html 反向的“view_event”与参数“()”和关键字参数“{}”未找到。 1模式尝试:['学生/日历/事件/(?P \\ W +)/ $'] – JoeJ 2015-02-23 19:48:40

+0

我解决了这个问题,因为代码有一个愚蠢的错字...但是,似乎当我点击事件的链接,它保持在同一页面上,只有网址发生变化。可能是什么原因? – JoeJ 2015-02-23 20:51:10