How to change the database name display in django admin site? - python

Is it possible to change change the database name in django admin site?
Just like how I change the following:
admin.site.site_header = "Administrator"
admin.site.site_title = "Administrator"
admin.site.index_title = "Admin"

There are two options, you can do it manually, or you can do it through the help of an external python library
Manually
is by creating custom AdminSite
admin.py
from django.contrib.admin import AdminSite
from django.utils.translation import ugettext_lazy
class MyAdminSite(AdminSite):
# Text to put at the end of each page's <title>.
site_title = ugettext_lazy('My site admin')
# Text to put in each page's <h1> (and above login form).
site_header = ugettext_lazy('My administration')
# Text to put at the top of the admin index page.
index_title = ugettext_lazy('Site administration')
admin_site = MyAdminSite()
urls.py
from django.conf.urls import patterns, include
from myproject.admin import admin_site
urlpatterns = patterns('',
(r'^admin/', include(admin_site.urls)),
)
by using python package
you can use the python package called django-admin-interface
pip install django-admin-interface
you'll need to import it into your settings.py
INSTALLED_APPS = (
#...
"admin_interface",
"flat_responsive", # only if django version < 2.0
"flat", # only if django version < 1.9
"colorfield",
#...
"django.contrib.admin",
#...
)
# only if django version >= 3.0
X_FRAME_OPTIONS = "SAMEORIGIN"
SILENCED_SYSTEM_CHECKS = ["security.W019"]
after installing it, you'll be able to customize your admin panel from the themes menu inside your admin panel
You can change the app name by adding verbose_name under app.py
from django.apps import AppConfig
class PharmacyConfig(AppConfig):
name = 'pharmacy'
verbose_name = 'Your Home Page'

Related

Django imagefield isn't showing any photo in the template

I'm working on a bookstore using Django. I'm trying to save each book's cover in each book created by the user using imagefield. I implemented every step in Django documentation about using imagefield but it doesn't work.
settings.py(in main project):
MEDIA_DIR = os.path.join(BASE_DIR,'media')
MEDIA_ROOT = MEDIA_DIR
MEDIA_URL = '/media/'
in urls.py (main project):
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("book.urls")),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
in models.py(the book app):
class books(models.Model):
cover = models.ImageField(upload_to='cover/co', blank=True)
When I want go admin I find that the photo is submitted (when I click on the photo url in database nothing is shown to me) but I don't find any created media folders and when I try to request the image in any template It isn't showed to me.
when I try to click on the photo in the database in admin this is shown to me:
enter image description here
These are the paths of the app, project and static:
enter image description here
I don't know where is the problem and how to solve it as this is my first time to use imagefiled in django model,,, it might be from the paths, the model or the urls so if there is any help.
Remove / at the beginning of your MEDIA_URL.
Simply you can try this way:
settings.py file:
MEDIA_URL='/media/'
MEDIA_ROOT=os.path.join(BASE_DIR,'media')
In models.py file:
class books(models.Model):
cover = models.ImageField(upload_to='cover/', blank=True) #I have removed co from here
After adding above code don't forget to migrate
Try this and see if this is solves your problem

Add view in the bottom of Django modeladmin

I have a blog made with Django where I write posts in markdown. I would like to add a view in the bottom of the admin page for each instance of the class Entry (my blog post class) such that I can get a preview of what the markdown looks like, while I'm writing. Just as you get a preview here on Stack Overflow when you create a new post.
I already have an admin class extending ModelAdmin:
class EntryAdmin(admin.ModelAdmin):
list_display = ('title','created')
prepopulated_fields = {'slug': ('title',)}
Is it possible to modify ModelAdmin further, such that it loads a certain html file (blogpost.html) and shows it in the bottom of the admin page?
I made a picture to show exactly what I mean:
NB: I know there are various tools such as Django admin plus, that allows one to add views to the admin interface, but not for each instance of an object.
You can use markdownx for that:
pip install django-markdownx
project settings.py
INSTALLED_APPS =
#. . . .
'markdownx',
]
project urls.py
urlpatterns = [
#[...]
url(r'^markdownx/', include('markdownx.urls')),
]
and then collect static files.
python3 manage.py collectstatic
your models.py
from markdownx.models import MarkdownxField
class MyModel(models.Model):
myfield = MarkdownxField()
your app admin.py
from django.contrib import admin
from markdownx.admin import MarkdownxModelAdmin
from .models import MyModel
admin.site.register(MyModel, MarkdownxModelAdmin)
This should work.

How to change django admin "view site" link to custom absolute url

I have REST API built on Django and JS application. Both are on different domains. How to change django admin "VIEW SITE" link in such way so it will open JS application? I've tried to pass absolute link (https://docs.djangoproject.com/es/1.10/ref/contrib/admin/#django.contrib.admin.AdminSite.site_url), but looks like it does not work - only relative paths allowed
I prefer resetting admin.site.site_url in urls.py, along with other site changes (instead of doing this in an admin.py file, cause a project can have serveral admin.py)
# urls.py
admin.site.site_url = '' # Removes the 'View Site' link
admin.site.site_header = 'My Site'
In Django 1.11.5, it seems that :
from django.contrib import admin and
admin.site.site_url = 'https:....' in the admin.py file is enough
By default , "VIEW SITE" points to '/' i.e localhost:8000 (Default settings assumed).
To change it , use (in admin.py):
admin.site.site_url = "/mySite"
Tested on Django 2.1
There are two solutions I can come up with.
Firstly, you could use custom template admin/base.html. But, reading through the default template, you would have to copy-paste a lot of code just to change a link, which seems like an overkill.
Another solution involves overriding AdminSite. AdminSite has a property called site_url, and it seems like changing it would do the job. So, in essense, you can do something like this:
your_app/admin.py
from django.contrib.admin import AdminSite
from .models import MyModel
class MyAdminSite(AdminSite):
site_url = 'https://yourdomain.com'
admin_site = MyAdminSite(name='myadmin')
your_project/urls.py
from django.conf.urls import url
from myapp.admin import admin_site
urlpatterns = [
url(r'^myadmin/', admin_site.urls),
]
And you should register all your models with your custom admin, not Django's default:
from your_app.admin import admin_site
admin_site.register(MyModel)
Default link to viewsite is http://127.0.0.1:8000/
We can change it to custom url through register it in admin.py
admin.site.site_url = "/mySite"
Example : admin.py
from django.contrib import admin
from auth_app.models import profile
# Register your models here.
admin.site.register(profile)
#register link
admin.site.site_url = "/mySite"
In admin.py, mention your custom path like following
admin.site.site_url = "/<Your Path>"
If your custom url is like "https://example.com/dashboard", You have to put below line in your admin.py
admin.site.site_url = "/dashboard"
You can change "VIEW SITE" link by changing "admin.site.site_url" which is "/" by default:
from django.contrib import admin
print(admin.site.site_url) # /
admin.site.site_url = "/example"

Django (1.10) override AdminSite

I've tried to override AdminSite class with my own custom class. I followed tutorial from django's documentation: https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#customizing-adminsite but it didn't work. To be specific, I'd like to override original AdminSite with my own class and not just add another admin site into my project.
I've created my custom class MyAdminSite which inherit from class
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
pass
Then in my app urls.py I've add:
from django.conf.urls import url, include
import django.contrib.admin as admin
from .admin_site import MyAdminSite
admin.site = MyAdminSite()
admin.autodiscover()
urlpatterns = [
url(r'^', admin.site.urls),
]
It seemed to work, but admin models are register to AdminSite insted of MyAdminSite.
I tried three ways of register models to my custom site:
#admin.register(Model)
class ModelAdmin(model.AdminModel):
...
This way models are registered to original AdminSite.
Second way:
#admin.site.register(Model):
class ModelAdmin(model.AdminModel):
...
That don't work and cause exception. The ModelAdmin class isn't passed to register method.
Last way:
class ModelAdmin(model.AdminModel):
...
admin.site.register(Model, ModelAdmin)
That works, but on admin site I can see only my models not models from Django admin (Users and Groups).
How can I permanently override admin.site and register all models to MyAdminSite?
From myapp/admin.py:
from django.contrib.auth.models import Group, User
from django.contrib.auth.admin import GroupAdmin, UserAdmin
from django.contrib.admin import AdminSite
from django.contrib import admin
from .models import MyModel #This is my app's model
# Custom admin site
class MyAdminSite(AdminSite):
site_header = 'My Project Title'
site_title = 'My Project Title Administration'
index_title = 'My Project Title Administration'
# You can add on more attributes if you need
# Check out https://docs.djangoproject.com/en/1.11/ref/contrib/admin/#adminsite-objects
# Create admin_site object from MyAdminSite
admin_site = MyAdminSite(name='my_project_admin')
# Register the models
class MyModelAdmin(admin.ModelAdmin):
list_display = ('id', 'description')
admin_site.register(MyModel, MyModelAdmin)
# Create and register all of your models
# ....
# This is the default Django Contrib Admin user / group object
# Add this if you need to edit the users / groups in your custom admin
admin_site.register(Group, GroupAdmin)
admin_site.register(User, UserAdmin)
From myproject/urls.py
from django.conf.urls import url
from django.contrib import admin
from myapp.admin import admin_site ##! Important..Import your object (admin_site) instead of your class (MyAdminSite)
urlpatterns = [
url(r'^admin/', admin_site.urls), #Now all /admin/ will go to our custom admin
]
I didn't found the solution to my problem, but I have made a workaround.
First we need to create module in our app (e.g. admin.py) and then extend class AdminSite:
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):
...
Then on bottom of module we need to create instance of our MyAdminSite and register built-in models from Django:
site = MyAdminSite()
site.register(Group, GroupAdmin)
site.register(User, UserAdmin)
Necessary imports:
from django.contrib.auth.models import Group, User
from django.contrib.auth.admin import GroupAdmin, UserAdmin
In our site url module we need to override original site object:
from .admin import site
admin.site = site
admin.autodiscover()
...
url(r'^admin/', admin.site.urls)
...
Last change we need to do is register our models. One thing we need to remeber is that we can't use register as decorator like that:
#admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
...
or:
#admin.site.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):
...
We need to define our ModelAdmin class and then call register on our MyAdminSite object:
class MyModelAdmin(admin.ModelAdmin):
...
admin.site.register(MyModel, MyModelAdmin)
This is the only solution that is working for me.
I faced a similar problem. I used Django 2.1 and the hook from the comments above didn't work for me. And also I was not able to import GroupAdmin and UserAdmin, like so
from django.contrib.auth.models import Group, User
from django.contrib.auth.admin import GroupAdmin, UserAdmin
Importing GroupAdmin or UserAdmin broke the code for some reason. I was not able to define the exact reason.
So my workaround was (in project/urls.py):
from django.conf.urls import include, url
from django.contrib.admin import site
from project.admin import myadmin
myadmin._registry.update(site._registry)
urlpatterns = [
url(r'^admin/', myadmin.urls),
]
The idea here is to copy registered models from default admin site. Maybe it's not good to do so, but I could not find anything else working.

is there any way to change the default text editor for textfield django in admin panel?

I am working on this project on django,i have this model which have few char fields and a text field. What i want to know is is there anyway i can edit the appearance of the text field in django admin panel so that i can edit what i write in that text field like the one in word press where you get options to make a text bold or insert links and images.
I tried this module called grappelli but it just changes the appearance of my admin panel adding colors and decorative stuffs.
class Heading(models.Model):
category = models.ForeignKey(Category)
title = models.CharField(max_length=5000)
content = models.TextField()
date = models.DateField(default=datetime.now())
def __str__(self):
return self.title
use ckeditor
https://github.com/django-ckeditor/django-ckeditor
on shell:
pip install django-ckeditor
on settings.py:
add 'ckeditor' on INSTALLED_APPS
on models.py:
from ckeditor.fields import RichTextField
class Heading(models.Model):
content = RichTextField()
You can use django-summernote
https://github.com/summernote/django-summernote
step 1: Install django-summernote
pip install django-summernote
step 2: Add django_summernote to INSTALLED_APP in settings.py.
INSTALLED_APPS = [
'django_summernote',
'...',
]
step 3: Add django_summernote.urls to project urls.py.
from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
# ...
urlpatterns = [
...
path('summernote/', include('django_summernote.urls')),
...
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
step 4: Give MEDIA_URL in your settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
step 5: on admin.py
from django.contrib import admin
from django_summernote.admin import SummernoteModelAdmin
from .models import Heading
class HeadingAdmin(SummernoteModelAdmin):
summernote_fields = ('content',)
admin.site.register(Heading, HeadingAdmin)
step 6: Run database migration
python manage.py migrate

Categories

Resources