8👍
This can be done, per model, as of 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)
7👍
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
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
# Disable View on Site link on admin page
site_url = None
- Django: how to aggregate / annotate over a many-to-many relationship?
- Django rest auth email instead of username
- Django REST framework: Can a nested object access its parent object's details in a List View?
- Django Queryset of related objects, after prefiltering on original model
- Django update one field using ModelForm
4👍
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:
<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. 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
django.jQuery( document ).ready(function($) {
$(".viewsitelink").parent().css('display', 'none')
});
Your admin.py could then look something like this:
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.
3👍
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
.
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 formset , queries for relational field for every form
- Commit manually in Django data migration
- Django filter through multiple fields in a many-to-many intermediary table
3👍
Django 2.0 above you can add in default admin
admin.site.site_url = None
Above trick worked for me very well.
1👍
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.
Just watch your back when you upgrade django / in general.
from django.contrib.auth.models import User
del User.get_absolute_url
- Unit tests fail after a Django upgrade
- Log all errors to console or file on Django site
- Django – Is storing objects in session a good practice?
- Heroku Upload – Could not find a version that satisfies the requirement anaconda-client==1.4.0
- Django: Duplicated logic between properties and queryset annotations
1👍
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:
class Media:
js = ['js/admin/user_change_form.js']
css = {'all': ('css/admin/user_change_form.css',)}
…static/css/admin/user_change_form.css
.change-form {
visibility: hidden;
}
…static/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')
});
- How can I create an encrypted django field that converts data when it's retrieved from the database?
- Enable oauth login with django-allauth but a custom provider
0👍
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
- Can I have some code constantly run inside Django like a daemon
- Django template keyword `choice_value` in no longer work in 1.11
0👍
Inside your app config (apps.py
) do this:
class MyAppConfig(AppConfig):
def ready(self):
admin.site.site_url = None
Works in Django 4.0 as well.
- Use javascript variable in django template
- Django redirect to another view with context
- Store browser tab specific data
- Django – what should you do if you don't need a database engine?