无反向匹配错误。如何调试?

发布于 2024-11-16 06:47:57 字数 256 浏览 2 评论 0原文

我正在尝试链接位于此处的 django 的 databrowse.admin 小部件:

http://127.0.0.1:8000/admin/openmaps/open_layers/

我尝试将其放入模板中,但它返回了反向匹配错误。如何调试?

<a href="{% url /admin/openmaps/open_layers/ %}">A</a>

I am trying to link the databrowse.admin widget of django that rests here :

http://127.0.0.1:8000/admin/openmaps/open_layers/

I tried to put this in a template and it returned a reverse match error. How to debug ?

<a href="{% url /admin/openmaps/open_layers/ %}">A</a>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

画尸师 2024-11-23 06:47:57

您尝试使用的 URL 标记在此处的 Django 文档(版本 1.4)中指定:

https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#url

其目的是保持链接中的 URL 干燥(不要重复),这样您就不会' t必须更改您的开发、登台、生产或您可能拥有的任何其他服务器环境之间的链接 URL。

url 标记将视图或通过 url 名称对视图的引用作为其主要参数,并将视图采用的任何参数作为第二个参数。来自文档:

{% url path.to.some_view v1 v2 %}

其中 path 是包名称,to 是模块名称,some_view 是视图函数。 v1v2 是视图采用的参数。它在 path/to.py 中看起来像这样:

from django.http import HttpResponse

def some_view(request, v1, v2):
    return HttpResponse("A response")

此外,在处理 admin 时,您需要使用 URL 命名空间策略来使用命名空间 admin ,如下所示:

{% url admin:view_name %}

您需要做什么找到您要查找的视图的路径,并使用该路径创建 URL。首先,您可以创建一个指向您的管理站点索引的链接,如下所示:

<a href="{% url admin:index %}">My Admin Site</a>

这些将分别为管理注销、密码更改表单和应用程序列表创建链接:

<a href="{% url admin:logout %}">Admin Logout</a>
<a href="{% url admin:password_change %}">Change Password</a>
<a href="{% url admin:app_list %}">The Application List</a>

对于管理中特定模型的视图,django 使用模型上的元数据用于构造其 url 名称。您可以对模型执行相同的操作以链接到其管理页面,但是,您需要以编程方式构造它们的名称(除非您知道它们)。因此,如果您有一个名为 Foo 的模型,您可以通过构造视图名称并使用 reverse 分别在管理中链接到其更改列表视图、添加视图和删除视图对它们的方法:

在您看来:

from django.core.urlresolvers import reverse

#...some view code...
#Get an instance of the model
bar = Foo.objects.all()[0]
prefix = "%s_%s_" % (Foo._meta.app_label, Foo._meta.module_name)
changelist_name = "%schangelist" % prefix
add_name = "%sadd" % prefix
delete_name = "%sdelete" % prefix

changelist_url = reverse(changelist_name)
add_url = reverse(add_name)
delete_url = reverse(delete_name, args=(bar.pk,)) #You need the id of the model you want to delete as an argument.

#...some more view code...

在您的模板中

<a href="{{ changelist_url }}">The Foo ChangeList</a>
<a href="{{ add_url }}">Add a Foo</a>
<a href="{{ delete_url }}">Delete {{ bar.name }}</a>

您可能需要深入研究 django 或您使用的任何特定扩展的内部才能获得您想要的确切 url 名称。如果您可以提供有关您尝试在管理员中访问的模型的更多详细信息,我可以提供更具体的答案。

The URL Tag you are attempting to use is specified in the Django Documentation here (for version 1.4):

https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#url

It's purpose is to keep the URLs in your links DRY (Don't Repeat Yourself), so that you don't have to change your link URLs between your dev, staging, production or any other server environments you may have.

The url tag takes a view or a reference to a view via a url name as its main argument, and any arguments that the view takes as second arguments. From the documentation:

{% url path.to.some_view v1 v2 %}

Where path is a package name, to is a module name and some_view is a view function. v1 and v2 are args that the view takes. It would look like so in path/to.py:

from django.http import HttpResponse

def some_view(request, v1, v2):
    return HttpResponse("A response")

Furthermore, when dealing with the admin, you'll need to use the namespace admin using the URL namespace strategy, like so:

{% url admin:view_name %}

What you need to do is find the path to the view you are looking for, and create the URL using that path. To get you started, you can create a link to you your admin site index like so:

<a href="{% url admin:index %}">My Admin Site</a>

These will create links for the admin logout, password change form, and app list, respectively:

<a href="{% url admin:logout %}">Admin Logout</a>
<a href="{% url admin:password_change %}">Change Password</a>
<a href="{% url admin:app_list %}">The Application List</a>

For the views on specific models within the admin, django uses the meta data on the models to construct their url names. You can do the same with your models to link to their admin pages, however, you'll need to construct their names programmatically (unless you know them). So if you had a model named Foo, you could link to its changelist view, add view, and delete view in the admin respectively by constructing their view names and using the reverse method on them:

In your view:

from django.core.urlresolvers import reverse

#...some view code...
#Get an instance of the model
bar = Foo.objects.all()[0]
prefix = "%s_%s_" % (Foo._meta.app_label, Foo._meta.module_name)
changelist_name = "%schangelist" % prefix
add_name = "%sadd" % prefix
delete_name = "%sdelete" % prefix

changelist_url = reverse(changelist_name)
add_url = reverse(add_name)
delete_url = reverse(delete_name, args=(bar.pk,)) #You need the id of the model you want to delete as an argument.

#...some more view code...

In your template

<a href="{{ changelist_url }}">The Foo ChangeList</a>
<a href="{{ add_url }}">Add a Foo</a>
<a href="{{ delete_url }}">Delete {{ bar.name }}</a>

You'll likely have to do some digging into the guts of django or any particular extension you're using to get the exact url name that you want. If you can provide more detail about the model you're trying to access in the admin, I can provide a more specific answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文