简体   繁体   English

在Django Admin中,如何强制“查看网站”按钮忽略网站?

[英]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.

相关问题 如何覆盖Django管理员的索引视图(sites.py)? - How can I override the index view (sites.py) for Django admin? Django:如何更改管理员按钮的“站点查看” URL - Django: How to change “view on site” url of admin button Django 管理员“保存并在现场查看”按钮 - Django admin "save and view on site" button 如何集成现有的MongoDB数据库以生成Django模型并在管理站点中查看? - How can I integrate existing MongoDB Database to generate Django Models and view in the admin site? 我可以使用 Django 管理面板作为站点管理员的唯一视图而不是开发人员吗? - Can I use Django admin panel as the only view for the admin of the site not for developer? 如何在 django 管理员上编辑“现场查看”url? - How to edit "view on site" url on django admin? 如何在 Django 管理站点中查看我的 MySQL 数据库? - How can I see my MySQL database in Django admin site? 如何在 Django admin 中隐藏(不禁用)ModelAdmin 列表视图中的操作添加模型按钮? - How can I hide (not disable) in Django admin the action add model button in ModelAdmin list view? 如何解决此 Django 管理问题? Django admin 站点匹配查询不存在 - How can I fix this Django admin issue? Django admin Site matching query does not exist 导入django.contrib.sites时无法访问Django管理站点 - Can't access Django admin site while import django.contrib.sites
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM