views:

161

answers:

1

I've got a couple of models. Neither have any list view other than their admin entries. For that reason, it's a bit of a pain to manually work out the URLs for model instances.

I would like to show a link on the listing and detail admin views that takes me directly to the public view. I can do the nonsense that works creates the URL but I don't know how to get it to show in the admin.

Any ideas?

+4  A: 

If the model has a get_absolute_url() method, there should automatically be a 'View on site' button at the top right of the admin detail screen.

For the list view, you can easily add a method to the list of fields shown:

class MyAdmin(admin.ModelAdmin):
    list_display=('name', 'anotherfield', 'show_url')

    def show_url(self, instance):
        return '<a href="%s">View on site</a>' % (instance.get_absolute_url())
    show_url.allow_tags = True
Daniel Roseman
Won't you need show_url.allow_tags = True ?
Dominic Rodger
Yes, just remembered it.
Daniel Roseman