Sunday, October 8, 2017

[Django] Enable Admin interface

 Python   Django   Admin  



▌Introduction


We will enable the default Admin interface in django.

▌Environment


▋Python 3.6.2
▋Django 1.8.18



▌Implement


▋Run Auth migration

First we have to run migration for Authentication.

$ python manage.py migrate auth

Which will create the following tables in database.





▋Update settings.py

Replace MIDDLEWARE=[…] with MIDDLEWARE_CLASSES=[…]

▋settings.py

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.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]


▋Create super user

Use the command to answer the question and create a super user as admin.

$ python manage.py createsuperuser




▋Login as Admin

Now go to http://localhost:8080/admin
And login to use the interface.

▋django_session not exists


If you get an error message like this,

(-2147352567, '發生例外狀況。', (0, 'Microsoft OLE DB Provider for SQL Server', "無效的物件名稱 'django_session'。", None, 0, -2147217865), None)
Command: SELECT (1) AS [a] FROM [django_session] WHERE [django_session].[session_key] = ? ORDER BY 1 OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY

Try migrate the database again with $ python manage.py migrate
Reference: Stackoverflow



▋Demo








▌Github





▌Reference





No comments:

Post a Comment