[Answer]-Django: How to change "view on site" url of admin button

1👍

You shouldn’t change files of third-party apps if you can avoid it, because you’ll essentially need to maintain a fork of the app. Instead of overriding get_absolute_url, you can override the object-tools block in the admin template. Something like this:

{% extends "admin/change_form.html" %}

{% block object-tools %}
{% if change %}{% if not is_popup %}
  <ul class="object-tools">
    {% block object-tools-items %}
    <li>
        {% url opts|admin_urlname:'history' original.pk|admin_urlquote as history_url %}
        <a href="{% add_preserved_filters history_url %}" class="historylink">{% trans "History" %}</a>
    </li>
    {% if has_absolute_url %}<li><a href="{% url 'your-custom-view-here' original.pk %}" class="viewsitelink">{% trans "View on site" %}</a></li>{% endif%}
    {% endblock %}
  </ul>
{% endif %}{% endif %}
{% endblock %}

Save this as templates/admin/photologue/<modelnamehere>/change_form.html (change <modelnamehere> to the model you want to customize)

In a future version of Django (1.7 I guess), you will be able to override the method view_on_site to do the same thing.

👤sk1p

0👍

Overriding get_absolute_URL() is proposed in the Django docs so it should be good to go.

See: Other model instance methods

Leave a comment