[Django]-How to change django admin "view site" link to custom absolute url

22👍

I prefer resetting admin.site.site_url in urls.py, along with other site changes (instead of doing this in an admin.py file, cause a project can have serveral admin.py)

# urls.py
admin.site.site_url = ''  # Removes the 'View Site' link
admin.site.site_header = 'My Site'

16👍

In Django 1.11.5, it seems that :
from django.contrib import admin and
admin.site.site_url = 'https:....' in the admin.py file is enough

11👍

By default , “VIEW SITE” points to ‘/’ i.e localhost:8000 (Default settings assumed).

To change it , use (in admin.py):

admin.site.site_url = "/mySite"

Tested on Django 2.1

5👍

There are two solutions I can come up with.

Firstly, you could use custom template admin/base.html. But, reading through the default template, you would have to copy-paste a lot of code just to change a link, which seems like an overkill.

Another solution involves overriding AdminSite. AdminSite has a property called site_url, and it seems like changing it would do the job. So, in essense, you can do something like this:

your_app/admin.py

from django.contrib.admin import AdminSite
from .models import MyModel

class MyAdminSite(AdminSite):
    site_url = 'https://yourdomain.com'

admin_site = MyAdminSite(name='myadmin')

your_project/urls.py

from django.conf.urls import url
from myapp.admin import admin_site

urlpatterns = [
    url(r'^myadmin/', admin_site.urls),
]

And you should register all your models with your custom admin, not Django’s default:

from your_app.admin import admin_site
admin_site.register(MyModel)

4👍

Default link to viewsite is http://127.0.0.1:8000/

We can change it to custom url through register it in admin.py

admin.site.site_url = "/mySite"

Example : admin.py

from django.contrib import admin
from auth_app.models import profile
# Register your models here.
admin.site.register(profile)
#register link
admin.site.site_url = "/mySite"

1👍

In admin.py, mention your custom path like following

admin.site.site_url = "/<Your Path>"

If your custom url is like "https://example.com/dashboard", You have to put below line in your admin.py

admin.site.site_url = "/dashboard"

1👍

You can change "VIEW SITE" link by changing "admin.site.site_url" which is "/" by default:

from django.contrib import admin

print(admin.site.site_url) # /

admin.site.site_url = "/example"

1👍

You should keep in mind that if you are setting the reference (in either a template or in the .py file) it is going to build the url assuming that you gave it a relative component (/mything becomes www.mysite.com/mything). If you want to go to a completely different url you must include the "https://" and give the full url.

1👍

reverse not work for urls route, you can config it like this:

from django.urls import include, path, reverse_lazy

admin.site.site_header = "MyAdmin"
admin.site.site_title = "MyAdmin"
admin.site.index_title = "MyAdmin Panel"
admin.site.site_url = reverse_lazy('dashboard:home')
👤Ershan

Leave a comment