Django 1.3 NoReverseMatch错误

时间:2011-10-04 09:47:06

标签: django django-templates django-urls

我的应用程序中出现以下错误(就我在Google上看到的情况而言很常见):

Caught NoReverseMatch while rendering: Reverse for 'add-post' with arguments '()' and keyword arguments '{}' not found.

问题是我第一次使用反向网址,所以我对可能导致此错误的原因感到有点迷茫,显然我已经把一切都搞定了。有人能告诉我发生了什么吗?

urls.py文件

urlpatterns = patterns('e_cidadania.apps.news.views',

    url(r'^add/$', 'add_post', name='add-post'),

    url(r'^(?P<post_id>\d+)/delete/$', DeletePost.as_view(), name='delete-post'),

    url(r'^(?P<post_id>\d+)/edit/$', 'edit_post', name='edit-post'),

    url(r'^(?P<post_id>\d+)', ViewPost.as_view(), name='view-post')

)

模板

[...]

{% if perms.news.add_post %}
    <div id="tools">
        <a href="{% url add-post %}">
            <img src="{{ STATIC_URL }}/assets/icons/add16.png" alt="{% trans 'Add new post' %}" title="{% trans 'Add new post' %}"/>
        </a>

[...]

新闻views.py(场地长140线,所以我剪了它)

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.contrib.auth.decorators import login_required, permission_required

# Generic class-based views
from django.views.generic.base import TemplateView, RedirectView
from django.views.generic.list import ListView
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic.detail import DetailView

from django.template import RequestContext
from django.views.generic.create_update import create_object
from django.views.generic.create_update import update_object

from django.contrib.auth.models import User
from e_cidadania.apps.spaces.models import Space
from e_cidadania.apps.news.models import Post
from e_cidadania.apps.news.forms import NewsForm    

@permission_required('news.add_post')
def add_post(request, space_name):

    """
    Create a new post. Only registered users belonging to a concrete group
    are allowed to create news. nly site administrators will be able to
    post news in the index page.
    """
    current_space = get_object_or_404(Space, url=space_name)
    form = NewsForm(request.POST or None)

    if request.method == 'POST':
        form_uncommited = form.save(commit=False)
        form_uncommited.author = request.user

        # Get space id
        place = Space.objects.get(url=space_name)
        form_uncommited.space = place

        # This should not be necessay since the editor filters the
        # script tags
        #if "<script>" in form_uncommited.post_message:
        #    return "SCRIPT TAGS ARE NOT ALLOWED"

        if form.is_valid():
            form_uncommited.save()
            return redirect('/spaces/' + space_name)

    return render_to_response('news/post_add.html',
                              {'form': form, 'get_place': current_space},
                              context_instance = RequestContext(request))

2 个答案:

答案 0 :(得分:2)

在您显示的信息中看不到任何明显的错误,也许您可​​以发布您的views.py?

确保您的urls.py中的每个条目都已正确定义,因为反向导入整个网址定义,如果出现错误(如尚未实现的视图),则会引发错误。

另外,你的views.py add_post签名是否与你的urls.py匹配?抱歉这个愚蠢的问题,但......确保;)

根据您的新信息进行编辑:

“def add_post(request,space_name):”

这需要将space_name传递给视图。从我看到的情况来看,你的反面并没有传递任何论据。另外,确保你的urls.py是正确的......你是不是在“add /”之后错过了space_name参数的模式?

答案 1 :(得分:0)

请尝试使用视图的路径:

{% url e_cidadania.apps.news.views.add_post %}