article

Sunday, June 11, 2017

Python Django - Admin Interface

Python Django - Admin Interface

Django provides a ready-to-use user interface for administrative activities. Django automatically generates admin UI based on your project models.

Starting the Admin Interface

The Admin interface depends on the django.countrib module. To have it working you need to make sure some modules are imported in the INSTALLED_APPS and MIDDLEWARE_CLASSES tuples of the myproject/settings.py file.

For INSTALLED_APPS make sure you have −

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

For MIDDLEWARE_CLASSES −

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

Before launching your server, to access your Admin Interface, you need to initiate the database −

C:\myprojectdjango>python manage.py migrate

migrate will create necessary tables or collections depending on your db type, necessary for the admin interface to run.

Create user

C:\myprojectdjango>python manage.py createsuperuser

input username, email address and password
Superuser created succesfully

Now to start the Admin Interface, we need to make sure we have configured a URL for our admin interface. Open the myproject/url.py and you should have something like −
 
from django.conf.urls import patterns, include, url
from django.contrib import admin

from django.contrib import admin
admin.autodiscover()

urlpatterns = [
    url(r'^admin/', admin.site.urls),
]

Now just run the server.

 C:\myprojectdjango>python manage.py runserver

 And your admin interface is accessible at: http://127.0.0.1:8000/admin/

login using created username and password

Related Post