简体   繁体   English

Django:删除管理员用户更改表单中的“现场查看”按钮

[英]Django: Remove "view on site" button in the admin User change form

get_absolute_url() method is cool, but in some cases is not needed. get_absolute_url() 方法很酷,但在某些情况下不需要。 django.contrib.auth.models.User has it set by default, this cause my projects to have a broken link in the admin. django.contrib.auth.models.User 默认设置了它,这会导致我的项目在管理中的链接断开。

How can I prevent that from happening?我怎样才能防止这种情况发生?

In one of my old projects I set a custom template in which I removed the html of the button, it doesn't sound like a good solution that would scale though.在我的一个旧项目中,我设置了一个自定义模板,在其中删除了按钮的 html,但这听起来不是一个可以扩展的好解决方案。 Anything better?有什么更好的吗?

If you click on Django 1.7 link, the site will tell you that "Its an insecure version of Django that is no longer supported. Please upgrade to a newer release!"如果您单击 Django 1.7 链接,该站点会告诉您“它是一个不安全的 Django 版本,不再受支持。请升级到更新的版本!”

For Django 1.9, following solution works fine as mentioned in the Django documentation对于 Django 1.9,如 Django 文档中所述,以下解决方案可以正常工作

in myapp/admin.py在 myapp/admin.py 中

from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
    # Disable View on Site link on admin page
    site_url = None

Instead of monkey patching you can hide the button on the client side using JavaScript.您可以使用 JavaScript 在客户端隐藏按钮,而不是猴子修补。 The HTML of the view on site button looks like this:现场视图按钮的 HTML 如下所示:

<li><a href="/admin/r/4/2/" class="viewsitelink">View on site</a></li>

If you just hide the anchor tag you will get part of the round button appearing as that is applied on the li tag.如果你只是隐藏锚标签,你会得到圆形按钮的一部分,因为它应用在 li 标签上。 Now unfortunately there is no easy way to use css to select that specific li tag since it doesn't have a class, name or id on it.不幸的是,现在没有简单的方法可以使用 css 来选择特定的 li 标签,因为它上面没有类、名称或 id。 So we can use jquery which gives you more control on your selectors.所以我们可以使用 jquery,它可以让您更好地控制选择器。 Put the following in your static folder.将以下内容放在您的静态文件夹中。 For example in the location static/admin/user_change_form.js例如在位置 static/admin/user_change_form.js

django.jQuery( document ).ready(function($) {
    $(".viewsitelink").parent().css('display', 'none')
});

Your admin.py could then look something like this:您的 admin.py 可能如下所示:

from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from django.contrib.admin import site

class CustomUserAdmin(UserAdmin):
    class Media:
        js = ['admin/user_change_form.js']

site.unregister(User)
site.register(User, CustomUserAdmin)

UPDATE更新

A feature was added in Django 1.7 ModelAdmin.view_on_site which lets you not display the “View on site” link.在 Django 1.7 ModelAdmin.view_on_site 中添加了一项功能,它允许您不显示“现场查看”链接。

This can be done, per model, as of django 1.7.从 django 1.7 开始,每个模型都可以做到这一点。

# myapp/admin.py
from django.contrib import admin
from myapp.models import MyModel

class MyModelAdmin(admin.ModelAdmin):
    view_on_site = False

admin.site.register(MyModel,MyModelAdmin)

I know this is old but I came across it when I had to do the same thing.我知道这很旧,但是当我不得不做同样的事情时我遇到了它。

A better solution would be to monkey patch it in an accounts/admin.py file when you have accounts in INSTALLED_APPS .当您在INSTALLED_APPSaccounts时,更好的解决方案是在accounts/admin.py文件中对它进行猴子修补。

admin.site.unregister(User)
# We don't want a broken View on Site link.  Thanks for that, contrib.auth!
del User.get_absolute_url
admin.site.register(User, MyUserAdmin)

Django 2.0 above you can add in default admin Django 2.0 以上可以添加默认管理员

admin.site.site_url = None

Above trick worked for me very well.以上技巧对我很有效。

As a last resort, I have a monkey_patch app at the bottom of my INSTALLED_APPS which modifies the built in django contrib apps in ways I haven't found better ways to modify such as username length, default admin forms, __unicode__ , etc.作为最后的手段,我在我的INSTALLED_APPS底部有一个monkey_patch应用程序,它以我没有找到更好的修改方式(例如用户名长度、默认管理表单、 __unicode__等)修改内置 django contrib 应用程序。

Just watch your back when you upgrade django / in general.当您升级 django / 时,请注意您的背部。

from django.contrib.auth.models import User
del User.get_absolute_url

I'm using Django 1.4 and Marwan Alsabbagh's solution worked fine for me.我正在使用 Django 1.4,而 Marwan Alsabbagh 的解决方案对我来说效果很好。 Although, when opening/refreshing User change form there was a short blink.虽然,当打开/刷新用户更改表单时,会有短暂的眨眼。 That is because JQuery hides this button only when page is loaded.那是因为 JQuery 仅在加载页面时隐藏此按钮。

To solve this minor issue I used CSS to hide the whole .change-form block.为了解决这个小问题,我使用 CSS 来隐藏整个 .change-form 块。 After page is loaded this block's visibility is restored by means of JQuery.页面加载后,该块的可见性通过 JQuery 恢复。 So, my code looks like this:所以,我的代码如下所示:

admin.py:管理.py:

class Media:
    js = ['js/admin/user_change_form.js']
    css = {'all': ('css/admin/user_change_form.css',)}

...static/css/admin/user_change_form.css ...静态/css/admin/user_change_form.css

.change-form {
    visibility: hidden;
}

...static/js/admin/user_change_form.js ...静态/js/admin/user_change_form.js

/* Do not show 'View on site' button in User change form */
django.jQuery( document ).ready(function($) {
    $(".viewsitelink").parent().css('display', 'none')
    /* restore visibility of the page (hidden in css to avoid blinking) */
    $(".change-form").css('visibility', 'visible')
});

在你的主应用程序的 url.py 中使用admin.site.site_url = "your url here"来修改 django 页面上的“访问站点” ,并在你的类中使用view_on_site = False和 display_list 来删除“view_on_site”

Inside your app config ( apps.py ) do this:在您的应用程序配置 ( apps.py ) 中执行以下操作:

class MyAppConfig(AppConfig):
    def ready(self):
        admin.site.site_url = None

Works in Django 4.0 as well.也适用于 Django 4.0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM