使用参数'('',)'和关键字参数'{}'找不到'event_detail'的反转。尝试过1种模式:['events /(?P <id> \\ d +)/ $']

时间:2015-10-06 04:15:04

标签: python django

我正在关注本教程http://slash4.net/blog/learn-django-in-4-hours,当我运行http://localhost:8000/

时,我在/得到此错误NoReverseMatch

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

感谢您的支持

2 个答案:

答案 0 :(得分:2)

编辑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。

因此,只需将home.html中的第55行更改为:

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

答案 1 :(得分:0)

您的模板上下文中没有event_id变量,您可能最想要的是:

<h4><a href="{% url 'event_detail' id=event.id %}">{{ event.title }}</a></h4>
相关问题