How do I create a dynamic link to a Django admin screen?

Asked

Viewed 29 times

-1

How to use the tags of the Django templates to create a dynamic link to the admin boards?

Let’s say the name of the admin panel is ProductsAdmin, and that this table was created and registered in the app store. And is available at the following url: /admin/store/products/

I would like to know how to generate a dynamic link to this screen, so avoid putting a link hardcoded in the template.

I tried that way and it didn’t work:

<a href="{% url 'admin:store:products' %}">
    Confira os produtos disponíveis
</a>

and the following error message was displayed:

django.urls.exceptions.NoReverseMatch: 'store' is not a registered namespace inside 'admin'

  • This namespace is not linked the urlspatterns message informs that it did not find in the "urls.py" routes.. path('suaRota/', algumaview.asview(), name="algum_name_defined").. show how your all to see how you can improve the result.

1 answer

0

When a class that inherits from the Django.contrib.admin class is registered, Django records the views associated with the following pattern:

  • Dashboard with the list: admin:<APP_NAME>_<CLASS_NAME>_changelist
  • Form for creation: admin:<APP_NAME>_<CLASS_NAME>_add

Thus, to create a link to the page described in the question would be as follows:

<a href="{% url 'admin:store_products_changelist' %}"> Link </a>
  • 1

    Just to add, if you want to use the model to take this data and make it a little more "dynamic" you could use Products._meta.app_label and Products._meta.model_name. to fill in APP_NAME and CLASS_NAME respectively.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.