mirror of
https://github.com/django/django.git
synced 2025-08-03 10:34:04 +00:00
Fixed #8261 -- ModelAdmin hook for customising the "show on site" button
``ModelAdmin.view_on_site`` defines wether to show a link to the object on the admin detail page. If ``True``, cleverness (i.e. ``Model.get_absolute_url``) is used to get the url. If it's a callable, the callable is called with the object as the only parameter. If ``False``, not link is displayed. With the aim of maitaining backwards compatibility, ``True`` is the default.
This commit is contained in:
parent
497930b7f6
commit
fd219fa24c
12 changed files with 266 additions and 10 deletions
|
@ -1091,6 +1091,37 @@ subclass::
|
|||
:meth:`ModelAdmin.get_search_results` to provide additional or alternate
|
||||
search behavior.
|
||||
|
||||
.. attribute:: ModelAdmin.view_on_site
|
||||
|
||||
.. versionadded:: 1.7
|
||||
|
||||
Set ``view_on_site`` to control whether or not to display the "View on site" link.
|
||||
This link should bring you to a URL where you can display the saved object.
|
||||
|
||||
This value can be either a boolean flag or a callable. If ``True`` (the
|
||||
default), the object's :meth:`~django.db.models.Model.get_absolute_url`
|
||||
method will be used to generate the url.
|
||||
|
||||
If your model has a :meth:`~django.db.models.Model.get_absolute_url` method
|
||||
but you don't want the "View on site" button to appear, you only need to set
|
||||
``view_on_site`` to ``False``::
|
||||
|
||||
from django.contrib import admin
|
||||
|
||||
class PersonAdmin(admin.ModelAdmin):
|
||||
view_on_site = False
|
||||
|
||||
In case it is a callable, it accepts the model instance as a parameter.
|
||||
For example::
|
||||
|
||||
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})
|
||||
|
||||
Custom template options
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue