开发者

No reverse match error. How to debug?

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

http://12开发者_开发知识库7.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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜