A django app that lets you add standard class-based views to the django admin index and navigation.
Project description
Django Custom Admin Pages
This app is in the process from being ported from an existing project into an installable app.
This app is currently in beta and only tested in django 3.2 / python 3.10
Installation
- Install the app from pypi
pip install django_custom_admin_pages
- Remove
django.contrib.admin
from your installed apps - In your django settings file add the following lines to your `INSTALLED_APPS``:
INSTALLED_APPS = [
# "django.contrib.admin", #REMOVE THIS LINE
# ...
"django_custom_admin_pages",
"django_custom_admin_pages.admin.CustomAdminConfig"
# ...
]
- If you've defined a custom django admin site, you can also subclass
django_custom_admin_pages.admin.CustomAdminConfig
:
from django_custom_admin_pages.admin import CustomAdminConfig
class MyCustomAdminSite(CustomAdminConfig):
pass
Usage
This app is for custom admin views which aren't associated with a specific model. Add dashboards and other custom admin views here.
To create a new custom admin view:
-
Create a class-based view in
django_custom_admin_pages.views
which inherits fromcustom_admin.views.admin_base_view.AdminBaseView
. -
Set the view class attribute
view_name
to whatever name you want displayed in the admin index. -
Register the view similar to how you would register a ModelAdmin using a custom admin function:
admin.site.register_view(YourView)
. -
Use the template
django_custom_admin_pages.templates.base_custom_admin.html
as a sample for how to extend the admin templates so that your view has the admin nav. -
Optional: Set the view class attribute
app_label
to the app you'd like the admin view to display in. This must match a label insettings.INSTALLED_APPS
. This will default to a new app calleddjango_custom_admin_pages
if left unset. -
Optional: Set the view class attribute
route_name
to manually override the automatically generated route_name inurlpatterns
.
See example_view.py
for more details. (It only routes in local dev)
Example:
## in django_custom_admin_pages.views.your_special_view.py
from django.contrib import admin
from django.views.generic import TemplateView
from django_custom_admin_pages.views.admin_base_view import AdminBaseView
class YourCustomView(AdminBaseView, TemplateView):
view_name="My Super Special View"
template_name="my_template.html"
route_name="some-custom-route-name" # if omitted defaults to snake_case of view_name
app_label="my_app" # if omitted defaults to "django_custom_admin_pages". Must match app in settings
# always call super() on get_context_data and use it to start your context dict.
# the context required to render admin nav-bar is included here.
def get_context_data(self, *args, **kwargs):
context:dict = super().get_context_data(*args, **kwargs)
# add your context ...
return context
admin.site.register_view(YourCustomView)
Your template should extend admin/base.html
so you don't lose the nav and admin styling:
<!--In your template-->
{% extends 'admin/base_site.html' %}
{% load static %}
{% block responsive %}
{{block.super}}
<!-- add any custom scripts and cdns here-->
{% endblock responsive %}
{% block title %} Example Admin View {% endblock %}
{% block content %}
<h1>Hello World</h1>
{% endblock %}
OR you can extend the base_custom_admin.html
template:
<!-- your_template.html -->
{% extends 'base_custom_admin.html' with title="your page title" %}
{% block content %}
<h1>Hello World</h1>
{% endblock %}
Example With Generic ListView:
## in django_custom_admin_pages.views.your_special_view.py
from django.contrib import admin
from django.views.generic import ListView
from django_custom_admin_pages.views.admin_base_view import AdminBaseView
class YourSpecialViewWithModels(AdminBaseView, ListView):
# Using the Team Model as an example
model: Team = Team
ordering = ['pk']
paginate_by = 5
context_object_name = "team"
view_name="My Super Special View With Models"
route_name="your_special_view_with_models"
template_name="my_template_with_models.html"
app_label="an_existing_app_in_your_project"
admin.site.register_view(YourSpecialViewWithModels)
<!-- template_with_models.html -->
{% extends 'admin/base_site.html' %}
{% block title %} Example Admin View With Models {% endblock %}
{% block content %}
<h1>Look at all these models:</h1>
{% for object in object_list %}
<h3>{{ object.name }} | {{ object.pk }}</h3>
{% endfor %}
{% endblock %}
Configurable Settings
CUSTOM_ADMIN_DEFAULT_APP_LABEL
: set to override the default app_label (default:django_custom_admin_pages
)
Contributing
Reach out to the author if you'd like to contribute! Also free to file bug reports or feature requests via github issues.
Local Development
To start the test_project:
cd <repo_root>
poetry install --with dev
python test_proj/manage.py migrate
python test_proj/manage.py createsuperuser
(follow prompts)python test_proj/manage.py runserver
- Navigate too
localhost:8000/admin
, log in, and there should be one custom admin view.
To run the test suite:
poetry run pytest
Prior to committing:
-
Run pylint:
cd <repo_root>
poetry run pylint django_custom_admin_pages/
-
Run black:
poetry run black .
-
Run isort:
poetry run isort django_custom_admin_pages/
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_custom_admin_pages-0.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0af98b3be3147c72e50bf1b95f410efb5fb428c5be46de8d6c4c5fbd62e6436a |
|
MD5 | be4ffae25ee19c75c66f52209e50934d |
|
BLAKE2b-256 | 66443cc093879ba780de67fbff126d5af64221293b316c7c43e12da5b209833c |
Hashes for django_custom_admin_pages-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98a87e3b3f4fe0b3f737098e9890820ee46578de3fe996681b857763205f5b2f |
|
MD5 | 4ff7d75b95df714f93e117be5376c7b1 |
|
BLAKE2b-256 | 607f9b4c1d35614d427ba0af0e3896df7e2032fcce83571892b3c281810bcfc6 |