English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Django Admin Interface

Form Django provides a user interface ready for use for management activities. We all know that the management interface is very important for a web project.

Django automatically generates the management interface based on your project model.

Start the management interface/The management interface depends on the django.contrib modules. To make it work, ensure that some modules are imported in the myproject

Ensure that INSTALLED_APPS is defined in the settings.py file. -

  INSTALLED_APPS = (
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'myapp',
)

For MIDDLEWARE_CLASSES there is−

  MIDDLEWARE_CLASSES = (
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.common.CommonMiddleware',
   'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware',
   'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

When starting the server, let's visit the admin interface, we may also need to start the database -

   c:\myproject> python manage.py syncdb

syncdb will create the necessary tables, or according to the set of your database type, and the necessary admin interface to run. Even if you are not a superuser, the system will prompt you to create one.

If you already have a superuser or have forgotten, you can use the following code to create one directly −

   c:\myproject> python manage.py createsuperuser

Now let's start the admin interface, we need to make sure that the URL for the admin interface is configured. Open myproject/url.py, should have something like this −

  """myproject URL Configuration"
The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.9/topics/http/urls/ 
Examples:
Function views
    1. Add an import: from my_app import views
    2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
. Include another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

The entire execution command process:

  # Create necessary database tables and initialize related data
C:\myproject>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying sessions.0001_initial... OK
C:\myproject> python manage.py createsuperuser
Username (leave blank to use 'administrator'): admin
Email address: [email protected] 
Password:
Password (again):
This password is too short. It must contain at least 8 characters.
This password is too common.
This password is entirely numeric.
Password:
Password (again):
Superuser created successfully.
C:\myproject>

Now, simply use the following command to start the server.

   c:\myproject> python manage.py runserver

The management interface should be accessible at the following URL: http://127.0.0.1:8000/admin/

Log in with the superuser account, and you will see the following interface −

This interface allows us to manage groups and users in Django, as well as all models registered in the application. This interface enables you to perform at least 'CRUD' (Create, Read, Update, Delete) model operations.