Django:删除“现场查看”管理用户更改表单中的按钮
get_absolute_url() 方法很酷,但在某些情况下不需要。 django.contrib.auth.models.User 默认设置它,这导致我的项目在管理中有一个损坏的链接。
我怎样才能防止这种情况发生?
在我的一个旧项目中,我设置了一个自定义模板,其中删除了按钮的 html,但这听起来不像是一个可以扩展的好解决方案。还有更好的吗?
get_absolute_url() method is cool, but in some cases is not needed. django.contrib.auth.models.User has it set by default, this cause my projects to have a broken link in the admin.
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. Anything better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
从 django 1.7 开始,每个模型都可以这样做。
This can be done, per model, as of django 1.7.
如果您单击 Django 1.7 链接,该网站会告诉您“这是不再受支持的 Django 不安全版本。请升级到更新版本!”
对于 Django 1.9,以下解决方案可以正常工作,如 Django 文档中所述
在 myapp/admin.py 中
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!"
For Django 1.9, following solution works fine as mentioned in the Django documentation
in myapp/admin.py
您可以使用 JavaScript 隐藏客户端上的按钮,而不是猴子修补。 “在网站上查看”按钮的 HTML 如下所示:
如果您只是隐藏锚标记,您将看到应用在 li 标记上的圆形按钮的一部分。不幸的是,现在没有简单的方法使用 css 来选择特定的 li 标签,因为它没有类、名称或 id。因此,我们可以使用 jquery,它可以让您更好地控制选择器。将以下内容放入静态文件夹中。例如,在 static/admin/user_change_form.js 位置,
您的 admin.py 可能如下所示:
更新
Django 1.7 中添加了一项功能 ModelAdmin.view_on_site 让您没有显示“现场查看”链接。
Instead of monkey patching you can hide the button on the client side using JavaScript. The HTML of the view on site button looks like this:
If you just hide the anchor tag you will get part of the round button appearing as that is applied on the li tag. 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. So we can use jquery which gives you more control on your selectors. Put the following in your static folder. For example in the location static/admin/user_change_form.js
Your admin.py could then look something like this:
UPDATE
A feature was added in Django 1.7 ModelAdmin.view_on_site which lets you not display the “View on site” link.
我知道这已经很旧了,但当我不得不做同样的事情时我遇到了它。
更好的解决方案是当您在
INSTALLED_APPS
中有accounts
时,在accounts/admin.py
文件中对其进行猴子修补。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 haveaccounts
inINSTALLED_APPS
.Django 2.0 上面你可以添加默认管理
上面的技巧对我来说非常有效。
Django 2.0 above you can add in default admin
Above trick worked for me very well.
作为最后的手段,我的
INSTALLED_APPS
底部有一个monkey_patch
应用程序,它可以修改内置的 django contrib 应用程序,我还没有找到更好的方法来修改此类应用程序如用户名长度、默认管理表单、__unicode__
等。一般来说,升级 django / 时请小心。
As a last resort, I have a
monkey_patch
app at the bottom of myINSTALLED_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.Just watch your back when you upgrade django / in general.
我正在使用 Django 1.4,Marwan Alsabbagh 的解决方案对我来说效果很好。不过,当打开/刷新用户更改表单时,会出现短暂的闪烁。这是因为 JQuery 仅在页面加载时隐藏此按钮。
为了解决这个小问题,我使用 CSS 隐藏整个 .change-form 块。页面加载后,该块的可见性将通过 JQuery 恢复。所以,我的代码如下所示:
admin.py:
...static/css/admin/user_change_form.css
...static/js/admin/user_change_form.js
I'm using Django 1.4 and Marwan Alsabbagh's solution worked fine for me. Although, when opening/refreshing User change form there was a short blink. That is because JQuery hides this button only when page is loaded.
To solve this minor issue I used CSS to hide the whole .change-form block. After page is loaded this block's visibility is restored by means of JQuery. So, my code looks like this:
admin.py:
...static/css/admin/user_change_form.css
...static/js/admin/user_change_form.js
使用
admin.site.site_url = "这里是您的网址"
在主应用程序的 url.py 中修改 django 页面上的“访问站点”
和
用于删除“view_on_site”
使用
view_on_site = False 在您的班级中,其中 display_list 为
use
admin.site.site_url = "your url here"
in url.py of ur main app to modify the "visit site" on django page
and
for "view_on_site" removal
use
view_on_site = False in ur class with display_list for
在您的应用程序配置 (
apps.py
) 中执行以下操作:也适用于 Django 4.0。
Inside your app config (
apps.py
) do this:Works in Django 4.0 as well.