2

Before I added the Sites application to my Django app, the "View on Site" button used a relative path. This was helpful because my site is accessed through several different domains and a staging environment.

I recently started using the Flat Pages application, which caused me to install the Sites application as well.

After this change, all "View on Site" buttons use the domain specified in my app's Default Site, instead of a path relative to the domain from which the Django Admin is accessed.

Is it possible to override the "View on Site" buttons (or the get_absolute_url() functions for each relevant model) to ignore the Sites domain, and go back to using relative paths?

bfox
  • 274
  • 2
  • 13
  • The answer provides a very good solution, it would be nice to accept it as correct in order to help people who have the same question. – raratiru Jan 20 '17 at 16:31

1 Answers1

2

Yes, to override the "View on Site" buttons add view_on_site method to your ModelAdmin.

Example from docs. ModelAdmin.view_on_site

from django.contrib import admin
from django.core.urlresolvers import reverse

class PersonAdmin(admin.ModelAdmin):
    def view_on_site(self, obj):
        return 'http://example.com' + reverse('person-detail',
                                              kwargs={'slug': obj.slug})
moonstruck
  • 2,729
  • 2
  • 26
  • 29
  • Thanks for the answer. It seems that `reverse()` returns URLs with the domain from the Site baked in, like `example.com/foo`. I'm trying to have it return just `/foo` instead. Do you know if this is possible? – bfox May 13 '15 at 20:05
  • Let's say I have a few apps in my Django project (post, account, etc), should we add this code to all our apps ? – Dr. Younes Henni Nov 13 '17 at 20:22