bfox
bfox

Reputation: 294

In the Django Admin, how can I force the "View on Site" button to ignore Sites?

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?

Upvotes: 2

Views: 1367

Answers (1)

moonstruck
moonstruck

Reputation: 2784

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})

Upvotes: 2

Related Questions