未找到带有参数“()”和关键字参数“”的“””

Posted

技术标签:

【中文标题】未找到带有参数“()”和关键字参数“”的“””【英文标题】:Reverse for '''' with arguments '()' and keyword arguments '' not found未找到带有参数“()”和关键字参数“”的“”” 【发布时间】:2012-12-05 13:05:32 【问题描述】:

请帮忙。我收到此错误:

“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 sn-p:

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

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

谢谢。

【问题讨论】:

这些错误通常很难找到。我通常禁用所有 urls.py 文件并一个接一个地启用它们,直到出现错误。那么这通常是视图本身的错误。 该错误应该指向发生这种情况的文件,您可以发布吗? @RickardZachrisson,我不确定这是否是您要求的:模板渲染期间出错在模板/Users/home/Desktop/Web Development/Aptana Studio 3.0/startthedark/templates/events/ night.html,第 15 行出错 你的 urls.py 是主 (conf) urls.py 吗?如果没有,您是否将这些 url 包含在 conf url 中? 你可以在这里发布或dpaste你的主要网址文件 【参考方案1】:

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

见changes-to-url-and-ssi

【讨论】:

我还必须在 url 周围添加单引号,即 % url ev_toggle_attendance % 变成 % url 'ev_toggle_attendance' % 我们是否必须在 django 1.4.3 中这样做

以上是关于未找到带有参数“()”和关键字参数“”的“””的主要内容,如果未能解决你的问题,请参考以下文章

未找到带有参数“(”,)”和关键字参数“”的“比率”的反向

未找到带有参数“()”和关键字参数“”的“登录”的反向操作

未找到带有参数“()”和关键字参数“”的“password_change_done”的反向

未找到带有参数“()”和关键字参数“”的“send_referral_code”的反向

未找到带有参数“()”和关键字参数“”的“guestbook.posts”的反向操作。尝试了 0 种模式:[]

未找到带有参数“()”和关键字参数“'pk':6”的“post_edit”的反向。尝试了 0 种模式:[]