[英]How to change django admin "view site" link to custom absolute url
[英]Django: How to change “view on site” url of admin button
在我的應用程序中,我使用的是django-photologue(第3部分應用程序),並且在其默認配置中,“查看網站”管理按鈕指向其模板。
我已經閱讀過“在網站上查看”按鈕使用get_absolute_url()模型函數來實現其行為。
我需要更改“在網站上查看”按鈕后面的網址。 我是否需要重寫get_absolute_url()或編輯其他內容?
提前致謝。
如果可以避免,則不應更改第三方應用程序的文件,因為本質上您將需要維護該應用程序的分支。 您可以覆蓋管理模板中的object-tools
塊,而不是覆蓋get_absolute_url
。 像這樣:
{% 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 %}
將其另存為templates/admin/photologue/<modelnamehere>/change_form.html
的模型templates/admin/photologue/<modelnamehere>/change_form.html
(將<modelnamehere>
更改為要自定義的模型)
在Django的未來版本(我想是1.7)中,您將可以重寫view_on_site
方法來執行相同的操作。
在Django文檔中提出了重寫get_absolute_URL()的建議,所以它應該很好。
請參閱: 其他模型實例方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.