Django 1.3 NoReverseMatch 错误
我的应用程序中出现以下错误(就我在 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))
I'm having the following error in my application (quite usual as far as I've seen on Google):
Caught NoReverseMatch while rendering: Reverse for 'add-post' with arguments '()' and keyword arguments '{}' not found.
The thing is that I'm using reverse urls for the first time, so I'm a bit lost about what could be causing this error, and apparently I have everything ok. Can someone tell me what's happening?
urls.py file
urlpatterns = patterns('e_cidadania.apps.news.views',
url(r'^add/
Template
[...]
{% 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>
[...]
News views.py (the fiel is 140 lines long, so I cut it)
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))
, 'add_post', name='add-post'),
url(r'^(?P<post_id>\d+)/delete/
Template
News views.py (the fiel is 140 lines long, so I cut it)
, DeletePost.as_view(), name='delete-post'),
url(r'^(?P<post_id>\d+)/edit/
Template
News views.py (the fiel is 140 lines long, so I cut it)
, 'edit_post', name='edit-post'),
url(r'^(?P<post_id>\d+)', ViewPost.as_view(), name='view-post')
)
Template
News views.py (the fiel is 140 lines long, so I cut it)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用视图的路径来代替:
Try using a path to the view instead:
在您显示的信息中看不到任何明显的错误,也许您可以发布您的views.py?
确保 urls.py 中的每个条目都正确定义,因为反向导入整个 url 定义,如果出现错误(例如尚未实现的视图),则会引发错误。
另外,您的views.py add_post签名是否与您的urls.py匹配?抱歉这个愚蠢的问题,但是......只是确保;)
根据您的新信息进行编辑:
“def add_post(request, space_name):”
这需要将 space_name 传递到视图。从我看来,你的反对并没有传递任何论据。另外,请确保您的 urls.py 是正确的...您是否缺少“add/”之后的 space_name 参数的模式?
Can't see any blatant error in the info you show, perhaps you can post your views.py?
Make sure that EVERY entry in your urls.py is correctly defined, since reverse imports the whole urls definition, and if there's an error (like a view not yet implemented), it will raise an error.
Also, does your views.py add_post signature matches your urls.py? Sorry for the dumb question but... just making sure ;)
Edited in light of your new info:
"def add_post(request, space_name):"
This requires space_name to be passed to the view. Your reverse isn't passing any arguments, from what I see. Also, make sure your urls.py is correct... aren't you missing the pattern for the space_name argument after "add/"?