djk2 / django-admin-shell

Django/Python shell for django admin site.
MIT License
77 stars 11 forks source link
console django django-admin python shell terminal

django-admin-shell

.. image:: https://badge.fury.io/py/django-admin-shell.svg :target: https://pypi.org/project/django-admin-shell/

.. image:: https://github.com/djk2/django-admin-shell/actions/workflows/tests.yaml/badge.svg?branch=master :target: https://github.com/djk2/django-admin-shell/actions/workflows/tests.yaml/ :alt: GitHub Actions

.. image:: https://requires.io/github/djk2/django-admin-shell/requirements.svg?branch=master :target: https://requires.io/github/djk2/django-admin-shell/requirements/?branch=master :alt: Requirements Status

Django application can execute python code in your project's environment on django admin site. You can use similar as python manage shell without reloading the environment.

Screens

.. image:: https://raw.githubusercontent.com/djk2/django-admin-shell/master/doc/static/screen1.png :alt: Django admin shell view

Shell in action

.. image:: https://raw.githubusercontent.com/djk2/django-admin-shell/master/doc/static/youtube.png :target: https://youtu.be/BnP2C3a-2cI

Install

  1. Install::

    pip install django-admin-shell

    or

    pip install git+https://github.com/djk2/django-admin-shell.git

    or after download zip

    pip install django-admin-shell.zip

  2. Add django_admin_shell to your INSTALLED_APPS setting

    settings.py ::

    INSTALLED_APPS = [ ... 'django_admin_shell', ... ]

  3. Add the django_admin_shell urls to your root url patterns (above admin/) :

    urls.py ::

    urlpatterns = [ re_path(r'^admin/shell/', include('django_admin_shell.urls')), ... re_path(r'^admin/', admin.site.urls), ]

Usage

.. note::

Make sure that in your project session is enable

More about session and how enabling session read here : https://docs.djangoproject.com/en/dev/topics/http/sessions/

Usually default session in django project is enable

Demo

Try django-admin-shell using simple demo app:

  1. Clone project

    git clone https://github.com/djk2/django-admin-shell.git

  2. Go to demo directory:

    cd django-admin-shell/demo

  3. Install requirements:

    pip install -r requirements.txt

  4. Run demo project

    ./manage.py runserver

  5. Go to "http://127.0.0.1:8000/admin/shell" and login to admin user with password admin

Settings

ADMIN_SHELL_ENABLE ^^^^^^^^^^^^^^^^^^^ type : bool

default : True

If shell is enable or disable. When application is disable then url: /admin/shell return Http404 Not found

ADMIN_SHELL_ONLY_DEBUG_MODE ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type : bool

default : True

If flag is set on True, then shell is available only in DEBUG mode.

If debug mode is required and debug mode is disabled then url: /admin/shell will return Http 403 Forbidden

ADMIN_SHELL_ONLY_FOR_SUPERUSER ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type : bool

default : True

If flag is set on True, then shell is available only for user with superuser permission.

If superuser is required and user not have permission then url: /admin/shell will return Http 403 Forbidden

ADMIN_SHELL_OUTPUT_SIZE ^^^^^^^^^^^^^^^^^^^^^^^^ type : integer

default : 250

Flag determines how many outputs can be remember.

ADMIN_SHELL_SESSION_KEY ^^^^^^^^^^^^^^^^^^^^^^^^ type : string

default : django_admin_shell_output

Name for key in session where is stored history last executed codes.

ADMIN_SHELL_IMPORT_DJANGO ^^^^^^^^^^^^^^^^^^^^^^^^^^^ type : bool

default : True

If flag is set on True, then useful libraries and packages from Django will be automatically imported to shell. For example: from django.conf import settings, so in shell you have directly access to attributes from this module (e.g settings.INSTALLED_APPS). List of automatically imported modules is displayed on top of console (screen below). If you want disable auto import for django packages, set this flag to False.

Nont: If during import occurred error ImportError then this module will be omitted.

.. image:: https://raw.githubusercontent.com/djk2/django-admin-shell/master/doc/static/auto_import.png :alt: Auto import section

ADMIN_SHELL_IMPORT_MODELS ^^^^^^^^^^^^^^^^^^^^^^^^^^^ type : bool

default : True

This flag is similar to ADMIN_SHELL_IMPORT_DJANGO but regarding auto import of models from all registered applications. If flag is set on True, then models from all apps will be automatically imported to shell. For example: from django.contrib.auth.models import Permission, Group, User, so in shell you have directly access to this classes. List of automatically imported models is displayed on top of console. If you want disable auto import for models, set this flag to False.

Nont: If during import occurred error ImportError then this module will be omitted.

ADMIN_SHELL_CLEAR_SCOPE_ON_CLEAR_HISTORY ^^^^^^^^^^^^^^^^^^^^^^^^^^^ type : bool

default : False

This flag is used to enable the gargabe collector on the declared variables from the shell execution when the "clear history" is executed. If this flag is set to True, then all the declared variables will be ERASED and FREED from memory on runtime when "clear history" is used. If you want to persist indefinitly all declared variables on the shell, set this flag to False.

BEWARE: leaving this disabled is not recomended on production code!

Code examples

Useful for me:

Similar projects: