How can I get the Django admin's "View on site" link to work?

Define a get_absolute_url on your model. The admin uses that method to figure out how to construct the objects url. See the docs.


Putting

'django.contrib.sites',

into your INSTALLED_APPS and a following

$ ./manage.py syncdb

may suffice.

When installed, edit the Site instance (e.g. through /admin interface) to reflect your local hostname (e.g. localhost:8000).


As communicated by others, this requires a couple extra steps in addition to enabling view_on_site. You have to implement get_absolute_url() in your model, and enable Sites in your project settings.

Set the view_on_site setting

Add view_on_site setting to admin form:

class MymodelAdmin(admin.ModelAdmin):
    ...
    view_on_site = True
...
admin.site.register(Mymodel, MymodelAdmin)

Implement get_absolute_url()

Add get_absolute_url() to your model. In models.py:

Mymodel(models.Model):
    ...
    def get_absolute_url(self):
        return "/mystuff/%i" % self.id

Enable Sites

Add Sites in yourapp/settings.py:

INSTALLED_APPS = (
    ...
    'django.contrib.sites',
    ...
)

Then update the database:

$ python manage.py migrate

Done!

Check out reverse() for a more sophisticated way to generate the path in get_absolute_url().