gpt4 book ai didi

反向()调用中的 Django NoReverseMatch

转载 作者:行者123 更新时间:2023-12-02 07:03:59 25 4
gpt4 key购买 nike

我又花了一个晚上在这上面...有人可以帮忙吗?

我正在尝试在 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)),
)

我的模块 url 包含:

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']

最佳答案

这一行:

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

您不需要终止符号 $,它会阻止包含工作。我总是不小心这样做。

关于反向()调用中的 Django NoReverseMatch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15422766/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com