0

I have django project. I want to use django admin site and have an url in django admin site to redirect user to certian url, so I used this code:

admin.py:

from django.contrib import admin

admin.site.site_url = 'http://localhost:8000/some-api-url/'

This code change VIEW SITE link in django admin. Problem is this url is static and I want to get server url dynamically, is there any approach to do that?

UPDATE:

I use postgres and neo4j, must of my models are in neo4j but some of them are in postgres. Django does not support neo4j models in admin site so we developed our frontend to support it. But django admin is complete so we want use it for our postgres models but for linking django admin to our site we need a link in django admin to our frontend. I think VIEW SITE link on top right of django admin site is a good link to use, I can change it, but I need a way to change it dynamically base on domain, so the question is how can I change 'VIEW SITE' url dynamically?

mastisa
  • 1,875
  • 3
  • 21
  • 39

1 Answers1

2

You would probably need to override AdminSite

I would do something like:

from django.contrib import admin

class MyAdminSite(admin.AdminSite):
    def each_context(self, request):
        context = super().each_context(request)
        context['site_url'] = self.generate_site_url(request)
        return context

   def generate_site_url(self, request):
       # Here goes your custom code for dynamic url
       return url
Kamil Niski
  • 4,580
  • 1
  • 11
  • 24
  • I tested it, the problem is default_site in AdminConfig is for django 2.1, is there any way for django 2.0? – mastisa Oct 14 '18 at 12:23
  • 1
    @Mastisa this should help for django 2.0 https://docs.djangoproject.com/pl/2.0/ref/contrib/admin/#customizing-the-adminsite-class – Kamil Niski Oct 14 '18 at 13:02
  • Thanks, it worked but there is another problem, how can I register Authentication and Authorization Users and Groups models to my admin site? – mastisa Oct 15 '18 at 04:45
  • @Mastisa Great, please accept it as an answer for future reference. If you have another issue it will be best to create a new question. – Kamil Niski Oct 15 '18 at 06:26
  • Your answer was helpful so I vote it, but it create another problem so it didn't solve my problem completely, by this answer I can only register my models, what can I do with django Users and Groups?? – mastisa Oct 15 '18 at 06:36
  • 1
    @Mastisa check this answer for registering User and Group https://stackoverflow.com/a/35003223/9820085 – Kamil Niski Oct 15 '18 at 06:46