无反向匹配错误。如何调试?
我正在尝试链接位于此处的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您尝试使用的 URL 标记在此处的 Django 文档(版本 1.4)中指定:
https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#url
其目的是保持链接中的 URL 干燥(不要重复),这样您就不会' t必须更改您的开发、登台、生产或您可能拥有的任何其他服务器环境之间的链接 URL。
url
标记将视图或通过 url 名称对视图的引用作为其主要参数,并将视图采用的任何参数作为第二个参数。来自文档:其中
path
是包名称,to
是模块名称,some_view
是视图函数。v1
和v2
是视图采用的参数。它在 path/to.py 中看起来像这样:此外,在处理 admin 时,您需要使用 URL 命名空间策略来使用命名空间
admin
,如下所示:您需要做什么找到您要查找的视图的路径,并使用该路径创建 URL。首先,您可以创建一个指向您的管理站点索引的链接,如下所示:
这些将分别为管理注销、密码更改表单和应用程序列表创建链接:
对于管理中特定模型的视图,django 使用模型上的元数据用于构造其 url 名称。您可以对模型执行相同的操作以链接到其管理页面,但是,您需要以编程方式构造它们的名称(除非您知道它们)。因此,如果您有一个名为
Foo
的模型,您可以通过构造视图名称并使用reverse
分别在管理中链接到其更改列表视图、添加视图和删除视图对它们的方法:在您看来:
在您的模板中
您可能需要深入研究 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:Where
path
is a package name,to
is a module name andsome_view
is a view function.v1
andv2
are args that the view takes. It would look like so in path/to.py:Furthermore, when dealing with the admin, you'll need to use the namespace
admin
using the URL namespace strategy, like so: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:
These will create links for the admin logout, password change form, and app list, respectively:
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 thereverse
method on them:In your view:
In your template
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.