Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate a url to a particular item in the Django Admin Site from a view?

Tags:

django

I would like to make a link that would take the user to a particular item in the admin site (assuming they have the correct permissions).

Something like: https://mysite/admin/app/model/id/

Can this be done with reverse?

like image 298
Alex Avatar asked Nov 08 '11 22:11

Alex


People also ask

How do I find my Django admin URL?

If you open a Django project's urls.py file, in the urlpatterns variable you'll see the line path('admin/', admin. site. urls) . This last path definition tells Django to enable the admin site app on the /admin/ url directory (e.g. http://127.0.0.1:8000/admin/ ).

How can I get previous URL in Django?

You can do that by using request. META['HTTP_REFERER'] , but it will exist if only your tab previous page was from your website, else there will be no HTTP_REFERER in META dict . So be careful and make sure that you are using . get() notation instead.

Can I use Django admin in production?

your company should follow a least access principle policy; so yes: only select people should have access. Django has basic auditing capability via signals and displayed in the admin out of the box. You can build on top of this.

How do I restrict access to admin pages in Django?

Django admin allows access to users marked as is_staff=True . To disable a user from being able to access the admin, you should set is_staff=False . This holds true even if the user is a superuser. is_superuser=True .


1 Answers

You can get the url in the view, using reverse,

object_change_url = reverse('admin:myapp_mymodel_change', args=(obj.id,))

Or in the template, using the url tag

{% url 'admin:myapp_mymodel_change' obj.id %}

or

{% load admin_urls %}
{% url opts|admin_urlname:'change' obj.id %}">

Note the above url tag syntax is for Django >= 1.5.

For more information, see the Django docs on reversing admin urls.

like image 151
Alasdair Avatar answered Sep 28 '22 05:09

Alasdair