反向()调用中的Django NoReverseMatch

时间:2013-03-15 00:39:03

标签: django python-2.7

我已经花了一个晚上......有人可以帮忙吗?

我试图复制http://lightbird.net/dbe/todo_list.html的kickstart示例,这个示例似乎是为早期版本的Django开发的(并没有全面描述操作方法)

无论如何,我管理任务直到“将任务标记为已完成”。然后我的问题开始了。

要清楚,一切都很好,直到我将“mark_done”添加到ItemAdmin类的list_display属性中。然后它似乎无法解析truc.todo.views.mark_done 我已经尝试了完整的绝对模块参考,以及你在下面看到的部分参考,但无济于事。 我更喜欢部分的(从“todo”开始),因为它意味着易于移植到其他项目。

感谢您的帮助。

我有一个文件树,如下所示:

truc/ (my project)
|-todo/ (my app)
| |-__init__.py
| |-views.py
| |-urls.py  (module urls)
| |-models.py
| |-templates/admin/todo/item/change_list.html
|-truc/
| |-urls.py  (main urls)
| |-settings.py
| |-etc.

我的主要网址包含:

from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^todo/$', include('todo.urls')),
    url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

我的模块网址包含:

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

urlpatterns = patterns('todo.views',
    url(r'^mark_done/(\d*)/$', 'mark_done'),
)

在models.py中我有:

from django.db import models
from django.contrib.auth.models import User
from django.contrib import admin

from django.utils.translation import ugettext as _
from django.utils.encoding import force_text
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse

[...]
class Item(models.Model):
    name = models.CharField(max_length=60)
    created = models.ForeignKey(DateTime)
    priority = models.IntegerField(default=0)
    difficulty = models.IntegerField(default=0)
    done = models.BooleanField(default=False)

    def mark_done(self):
### The below is THE line which I cannot have work...
        return "<a href='%s'>Done</a>" % reverse("todo.views.mark_done", args=[self.pk])
    mark_done.allow_tags = True

class ItemAdmin(admin.ModelAdmin):
    list_display = ["name", "priority", "difficulty", "created", "mark_done", "done"]
    search_fields = ["name"]
[...]

错误和回溯:

NoReverseMatch at /admin/todo/item/
Reverse for 'todo.views.mark_done' with arguments '(5,)' and keyword arguments '{}' not found.
Request Method: GET
Request URL:    http://127.0.0.1:8000/admin/todo/item/
Django Version: 1.5
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'todo.views.mark_done' with arguments '(5,)' and keyword arguments '{}' not found.
Exception Location: /Library/Python/2.7/site-packages/django/core/urlresolvers.py in _reverse_with_prefix, line 416
Python Executable:  /usr/bin/python
Python Version: 2.7.1

[...]
Template error:
In template /Users/arnaud/Documents/truc/todo/templates/admin/todo/item/change_list.html, error at line 92
   Reverse for 'todo.views.mark_done' with arguments '(5,)' and keyword arguments '{}' not found.
   82 :         {% endif %}


   83 :       {% endblock %}


   84 : 


   85 :       <form id="changelist-form" action="" method="post"{% if cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %}>{% csrf_token %}


   86 :       {% if cl.formset %}


   87 :         <div>{{ cl.formset.management_form }}</div>


   88 :       {% endif %}


   89 : 


   90 :       {% block result_list %}


   91 :           {% if action_form and actions_on_top and cl.full_result_count %}{% admin_actions %}{% endif %}


   92 :            {% result_list cl %} 


   93 :           {% if action_form and actions_on_bottom and cl.full_result_count %}{% admin_actions %}{% endif %}


   94 :       {% endblock %}


   95 :       {% block pagination %}{% pagination cl %}{% endblock %}


   96 :       </form>


   97 :     </div>


   98 :   </div>


   99 : {% endblock %}


   100 : 

Traceback:
File "/Library/Python/2.7/site-packages/django/core/handlers/base.py" in get_response
  140.                     response = response.render()
File "/Library/Python/2.7/site-packages/django/template/response.py" in render
  105.             self.content = self.rendered_content
File "/Library/Python/2.7/site-packages/django/template/response.py" in rendered_content
  82.         content = template.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  140.             return self._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
  124.         return compiled_parent._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
  124.         return compiled_parent._render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in _render
  134.         return self.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
  63.             result = block.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/loader_tags.py" in render
  63.             result = block.nodelist.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  830.                 bit = self.render_node(node, context)
File "/Library/Python/2.7/site-packages/django/template/debug.py" in render_node
  74.             return node.render(context)
File "/Library/Python/2.7/site-packages/django/template/base.py" in render
  1185.                     _dict = func(*resolved_args, **resolved_kwargs)
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in result_list
  286.             'results': list(results(cl))}
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in results
  264.             yield ResultList(None, items_for_result(cl, res, None))
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in __init__
  256.         super(ResultList, self).__init__(*items)
File "/Library/Python/2.7/site-packages/django/contrib/admin/templatetags/admin_list.py" in items_for_result
  184.             f, attr, value = lookup_field(field_name, result, cl.model_admin)
File "/Library/Python/2.7/site-packages/django/contrib/admin/util.py" in lookup_field
  258.                 value = attr()
File "/Users/arnaud/Documents/truc/todo/models.py" in mark_done
  76.         return "<a href='%s'>Done</a>" % reverse("todo.views.mark_done", args=[self.pk])
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in reverse
  496.     return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py" in _reverse_with_prefix
  416.                 "arguments '%s' not found." % (lookup_view_s, args, kwargs))

Exception Type: NoReverseMatch at /admin/todo/item/
Exception Value: Reverse for 'todo.views.mark_done' with arguments '(5,)' and keyword arguments '{}' not found.

urlresolvers.py的_reverse_with_prefix中的本地变量:

candidate   u'/todo/mark_done/5/'
val 5
pattern 'todo/$mark_done/(\\d*)/$'
self    <RegexURLResolver 'truc.urls' (None:None) ^/>
args    (5,)
_prefix u'/'
m   'todo.views'
possibility [(u'todo/mark_done/%(_0)s/', [u'_0'])]
lookup_view_s   u'todo.views.mark_done'
possibilities   [([(u'todo/mark_done/%(_0)s/', [u'_0'])], 'todo/$mark_done/(\\d*)/$', {})]
lookup_view <function mark_done at 0x10da242a8>
prefix_norm u'/'
prefix_args []
params  [u'_0']
result  u'todo/mark_done/%(_0)s/'
defaults    {}
kwargs  {}
n   'mark_done'
unicode_args    [u'5']

2 个答案:

答案 0 :(得分:6)

这一行:

url(r'^todo/$', include('todo.urls'))

你不想要终止符号$,它阻止了包含工作。我不小心一直这样做。

答案 1 :(得分:2)

您需要为您的网址格式提供一个名称:

urlpatterns = patterns('todo.views',
    url(r'^mark_done/(\d+)/$', 'mark_done', name='mark_done'),
)

并从“todo”模式中删除结束模式$,导致它停止:

urlpatterns = patterns('',
    (r'^todo/', include('todo.urls')),
    (r'^admin/doc/', include('django.contrib.admindocs.urls')),
    (r'^admin/', include(admin.site.urls)),
)

然后你可以使用reverse,传入名称和参数:

def mark_done(self):
    return "<a href='{0}'>Done</a>".format(reverse("mark_done", args=(self.pk,)))
mark_done.allow_tags = True

我假设你的代码示例中没有显示名为mark_done的视图函数。