New django admin url, regardless of application

I am using django 1.4 and Python 2.7.

I just have a simple requirement when I have to add a new url to the django admin application. I know how to add URLs that are intended for custom applications, but I cannot figure out how to add URLs from an admin application. Please check me out through this.

Basically, the full URL should be something like admin/my_url .

UPDATE

I need a way after which I can also change the URL map using admin.

+6
source share
2 answers

+1 for Jingo answer to your original question. With your clarifying comment for the answer in mind: This URL is not "application independent", it is the "admin" application URL.

Adding the URL to the admin site is similar to ModelAdmin by overriding get_urls (): https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-views-to-admin-sites

EDIT:

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.AdminSite

is an administrative site, by default, the β€œadmin site is created asβ€œ django.contrib.admin.site ”(and then, for example, your ModelAdmin is registered on this). So you can subclass AdminSite for your own MyAdminSite and override get_urls () there:

 from django.contrib.admin import AdminSite class MyAdminSite(AdminSite): def get_urls(): ... ... my_admin_site = MyAdminSite() ... my_admin_site.register(MyModel, MyModelAdmin) 

Make sure you now use my_admin_site in urls.py: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#hooking-adminsite-instances-into-your-urlconf

Regarding the actual contents of get_urls (), see https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls (of course, calling super () MyAdminSite). Also pay attention to the convenient admin_view wrapper specified there.

PS: In theory, you can also simply define get_urls () and then monkeypatch the default admin site so that it uses your get_urls (), but I don’t know if this really works - you will probably have to disable it right after it’s first "import ...

+5
source

Just put your desired url mapping before the admin mapping in root urls.py. The first match for the request will be accepted, because django sends url mappings from top to bottom. Just remember that you are not using the URL that the administrator usually needs or provides, because this will never match the custom mapping before it. NTN!

+1
source

Source: https://habr.com/ru/post/919472/


All Articles