我下面這個教程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支持
其觀點將呈現模板,哪裏'event_id'從何而來? – ozgur
順便說一句,'/'在你的url conf中缺少。應該是'url(r'^/$','events.views.home',name ='home')' – ozgur