0

请帮忙。我收到此错误:

未找到带有参数“()”和关键字参数“{}”的“ev_toggle_attendance”的反向操作。

这是我的html文件:

    {% extends "base.html" %}

{% block title %}Tonight - {{ block.super }}{% endblock %}

{% block main_content %}
    <a href ="{% url ev_create %}">Create an Event</a>
    {% if events %}
        {% for event, attending in events %}
            <p>{{event.creator.username}}: {{event.description}}</p>
            {% if attending %}
                <p> You are attending this event.</p>
            {% else %}
                <p> You are not attending this event. </p>
            {% endif %}
            <form method="POST" class="toggle_attendance_form" action="{% url ev_toggle_attendance %}">
                <input type="hidden" name="event_id" value="{{event.id}}"/>
                {% if attending %}
                    <input class="attendance unattend" type="submit" value="Unattend"/>
                {% else %}
                    <input class="attendance attend" type="submit" value="Attend"/>
                {% endif %}
            </form>

        {% endfor %}
    {% else %}
        <p>No events for today.</p>
    {% endif %}
{% endblock %}

它说错误在这里:

<form method="POST" class="toggle_attendance_form" action="{% url ev_toggle_attendance %}">

urls.py 文件:

from django.conf.urls import patterns, include, url
from events import views

urlpatterns = patterns('',
    url(r'^create/$', views.create, name='ev_create'),
    url(r'^tonight/$', views.tonight, name='ev_tonight'), #ev_tonight is the name of the url pattern; good for templates
    url(r'^toggle-attendance/$', views.toggle_attendance, name='ev_toggle_attendance'),
)

views.py 片段:

from events.models import Event, Attendance
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from events.forms import EventForm
from dateutil.parser import parse
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, Http404
from django.contrib import messages
from django.contrib.auth.decorators import login_required


# Create your views here.

def tonight(request):
    events = Event.objects.today().filter(latest=True)
    #loop thru all the events and check to see if the currently logged in user is attending the event
    attending=[]
    for event in events: #for every event in all the events, query for an attendance objects that matches event for the user
        try:
            Attendance.objects.get(event=event, user=request.user)
            attending.append(True)
        except Attendance.DoesNotExist: #if can't find it
            attending.append(False)
    context = {
        'events':zip(events, attending), #zip function to merge event and attending
     }
    return render_to_response('events/tonight.html', context,
                              context_instance=RequestContext(request) )

    #context_instance: context processors: things that get run on request and add context to it; does things like adding the user variable to the template. 

def create(request):
    form = EventForm(request.POST or None)
    if form.is_valid():#check if there's data as well as within the 340 characters limit
        event = form.save(commit = False) #won't save to database coz creator isn't yet known
        event.creator = request.user
        guessed_date = None
        for word in event.description.split(): #split on every space - guess the date based on things inside the description
            try:
                guessed_date = parse(word)
                break#if found, break
            except ValueError:
               continue
        event.start_date = guessed_date
        event.save() #saves to the database
        messages.add_message(request, messages.INFO, 'Your event was posted.')
        #redirect user to another url
        if 'next' in request.POST: #if somewhere in the post value, there's a next
            next = request.POST['next']
        else:
            next = reverse('ev_tonight') #otherwise, next will be the url with ev_tonight
        return HttpResponseRedirect(next)

    #if the form isn't valid
    return render_to_response(
          'events/create.html',
          {'form':form},
          context_instance = RequestContext(request))
create = login_required(create) #ensuring that the user logged in or can do @login_required before the create model(python >= 2.4)

    def toggle_attendance(request):
        try:
            event_id = int(request.POST['event_id'])#assuming there are values in the post parameter; cast into an it
        except (KeyError, ValueError): #raise an exception when: no event id in the post parameter or a value that could not be cast to an int
            raise Http404
        event = get_object_or_404(Event, id=event_id)
        attendance, created = Attendance.objects.get_or_create(user=request.user, event=event)#calling for the user that is logged in and for the event that has already been queried for

        if created:
            messages.add_message(request, messages.INFO, 'You are now attending "%s"' %event)

        else:
            attendance.delete()
            messages.add_message(request, messages.INFO, 'You are no longer attending "%s"' %event)

        #checking to see if there's a next variable in the post parameter; if it isn't assign an empty string
        next=request.POST.get('next', '')
        if not next: #if it's the empty string
            next=reverse('ev_tonight')
        return HttpResponseRedirect(next)
    toggle_attendance = login_required(toggle_attendance)

主网址文件:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^events/', include('events.urls')),
    # Uncomment the next line to enable the admin:
    url(r'^admin/', include(admin.site.urls)),
)

非常感谢您提供的任何帮助。

谢谢。

4

1 回答 1

1

尝试从未来的模板标签加载 url:{% load url from future %}

查看对 url 和 ssi 的更改

于 2012-12-18T16:45:38.660 回答