2015-10-06 66 views
-1

我下面這個教程http://slash4.net/blog/learn-django-in-4-hours,和我在/得到這個錯誤NoReverseMatch,當我運行http://localhost:8000/

urls.py

urlpatterns = [ 
    url(r'^admin/', include(admin.site.urls)), 
    url(r'^$', 'events.views.home', name='home'), 
    url(r'^events/$', 'events.views.list', name='event_list'), 
    url(r'^events/(?P<id>\d+)/$', 'events.views.detail', name='event_detail'), 

    url(r'^register/$', 'events.views.register', name='register'), 
    url(r'^login/$', auth_view.login, name='login', kwargs={'template_name': 'users/login.html'}), 
    url(r'^logout/$', auth_view.logout, name='logout', kwargs={'next_page': '/'}), 

    url(r'^events/join/(?P<event_id>\d+)/$', 'events.views.join', name='event_join'), 
    url(r'^events/cancel/(?P<event_id>\d+)/$', 'events.views.cancel', name='event_cancel'), 
    url(r'^user_event/(?P<user_id>\d+)/$', 'events.views.user_event', name='user_event') 
] 

的模板,模板渲染,在錯誤行過程中出現錯誤55

這是第55行:

<h4><a href="{% url 'event_detail' event_id %}">{{ event.title }}</a></h4> 

views.py

from datetime import datetime 
from django.shortcuts import render 

from django.contrib.auth.forms import UserCreationForm 
from django.http import HttpResponseRedirect 

from django.contrib.auth.decorators import login_required 
from django.core.urlresolvers import reverse 

from django.contrib.auth.models import User 

from .models import Event 

def home(request): 
    try: 
     event = Event.objects.filter(date__gt=datetime.now()).order_by('date')[:1][0] 
    except: 
     event = [] 
    return render(request, 'events/home.html', {'event': event}) 

def list(request): 
    event_list = Event.objects.all().order_by('date'); 
    return render(request, 'events/list.html', {'event_list': event_list}) 

def detail(request, id): 
    event = Event.objects.get(id=id) 
    joined = event.guest.filter(id=request.user.id) 
    return render(request, 'events/detail.html', {'event': event, 'joined': joined}) 

def register(request): 
    if request.method == 'POST': 
     form = UserCreationForm(request.POST) 
     if form.is_valid(): 
      new_user = form.save() 
      return HttpResponseRedirect("/") 
    else: 
     form = UserCreationForm() 
    return render(request, "users/register.html", { 
    'form': form, 
    }) 

@login_required 
def join(request, event_id): 
    try: 
     # already joined 
     event = Event.objects.get(id=event_id, guest=request.user) 
     message = "You have already joined this event" 
    except Event.DoesNotExist as e: 
     # Event exist but joined 
     try: 
      event = Event.objects.get(id=event_id) 
      event.guest.add(request.user) 
      event.save() 
      message = "You have joined this event" 
     except Event.DoesNotExist as e: 
      message = "Error on event joining" 

    url = "%s?msg=%s" % (reverse('event_detail', args=[event_id]), message) 
    return HttpResponseRedirect(url) 

@login_required 
def cancel(request, event_id): 
    try: 
     event = Event.objects.get(id=event_id, guest=request.user) 
     event.guest.remove(request.user) 
     event.save() 
     message = "Your request not to attend has been saved" 
    except Event.DoesNotExist as e: 
      message = "Error on cancelling your attedance on event" 

    url = "%s?msg=%s" % (reverse('event_detail', args=[event_id]), message) 
    return HttpResponseRedirect(url) 

def user_event(request, user_id): 
    event_list = Event.objects.filter(guest__id=user_id) 
    user = User.objects.get(id=user_id) 

    try: 
     event_list = Event.objects.filter(guest__id=user_id) 
     user = User.objects.get(id=user_id) 
    except: 
     event_list = [] 
     user = {} 

    print(event_list) 

    return render(request, 'events/user_event.html', {'event_list': event_list, 'user': user}) 

感謝UR支持

+1

其觀點將呈現模板,哪裏'event_id'從何而來? – ozgur

+0

順便說一句,'/'在你的url conf中缺少。應該是'url(r'^/$','events.views.home',name ='home')' – ozgur

回答

0

編輯1
我克隆了這個項目,問題是:
這個錯誤是由home.html 因爲視圖代碼

def home(request): 
    try: 
     event = Event.objects.filter(date__gt=datetime.now()).order_by('date')[:1][0] 
    except: 
     event = [] 
    return render(request, 'events/home.html', {'event': event}) 

列表是空沒有一個事件被傳遞到home.html的。

所以,只是改變了線55 home.html,到:

{%if event%} 
    <h4><a href="{% url 'event_detail' event.id %}">{{ event.title }}</a></h4> 
{%endif%} 
+0

我試過他們兩個,但它仍然不工作 –

+0

你可以編輯帖子,與模板代碼?以及該應用的名稱是什麼? –

+0

這個應用程序的名稱:事件,請檢查從github的代碼,我已經從https://github.com/slash4-de/django克隆它 –

0

你沒有event_id變量在模板方面,你最想:

<h4><a href="{% url 'event_detail' id=event.id %}">{{ event.title }}</a></h4> 
+0

它仍然不起作用,同樣的錯誤 –

+0

'event_detail'需要'id'作爲kwarg而不是'event_id' – ozgur

+0

不知道爲什麼這是downvoted ..看起來對我來說是正確的 – Sayse

相關問題