[英]In the Django Admin, how can I force the “View on Site” button to ignore Sites?
Before I added the Sites application to my Django app, the "View on Site" button used a relative path. 在将“站点”应用程序添加到我的Django应用程序之前,“查看站点”按钮使用了相对路径。 This was helpful because my site is accessed through several different domains and a staging environment.
这很有用,因为可以通过几个不同的域和暂存环境访问我的网站。
I recently started using the Flat Pages application, which caused me to install the Sites application as well. 我最近开始使用Flat Pages应用程序,这也导致我也安装了Sites应用程序。
After this change, all "View on Site" buttons use the domain specified in my app's Default Site, instead of a path relative to the domain from which the Django Admin is accessed. 进行此更改后,所有“在站点上查看”按钮均使用我的应用程序默认站点中指定的域,而不是相对于访问Django Admin的域的相对路径。
Is it possible to override the "View on Site" buttons (or the get_absolute_url()
functions for each relevant model) to ignore the Sites domain, and go back to using relative paths? 是否可以覆盖“在网站上查看”按钮(或每个相关模型的
get_absolute_url()
函数)以忽略Sites域,然后返回使用相对路径?
Yes, to override the "View on Site" buttons add view_on_site method to your ModelAdmin. 是的,要覆盖“在站点上查看”按钮,请将view_on_site方法添加到您的ModelAdmin中。
Example from docs. 来自文档的示例。 ModelAdmin.view_on_site
ModelAdmin.view_on_site
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})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.