Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - How to get admin url from model instance

I'm trying to send an email to a user when a new model instance is saved and I want the email to include a link to the admin page for that model instance. Is there a way to get the correct URL? I figure Django must have that information stored somewhere.

like image 829
Greg Avatar asked May 02 '12 19:05

Greg


People also ask

How do I get to the admin page in Django?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).

Where is admin HTML Django?

To view the default admin template you can access it in the django/contrib/admin/templates/admin folder.

What is absolute URL Django?

To get the full or absolute URL (with domain) in Python Django, we can use the build_absolute_uri method. request.build_absolute_uri(reverse('view_name', args=(obj.pk, ))) to call request. build_absolute_uri with reverse('view_name', args=(obj.pk, ) to get the path of the view with reverse .


1 Answers

Not trying to rip off @JosvicZammit, but using ContentType is the wrong approach here. It's just a wasted DB query. You can get the require info from the _meta attribute:

from django.urls import reverse  info = (model_instance._meta.app_label, model_instance._meta.model_name) admin_url = reverse('admin:%s_%s_change' % info, args=(model_instance.pk,)) 
like image 187
Chris Pratt Avatar answered Sep 24 '22 16:09

Chris Pratt