Django 1.3 NoReverseMatch error
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/$', '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')
)
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 List开发者_StackOverflow中文版View
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))
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/"?
Try using a path to the view instead:
{% url e_cidadania.apps.news.views.add_post %}
精彩评论