Setting Django Admin Area Django.How

Author avatar wrote on 26/05/2022

  1. Type python createsuperuser

  2. Enter the preferred username, email, password

  1. Run the server python runserver

  2. Click on the provided link then add /admin like and login

Allowing Managing Questions and Answers Using Admin Panel

Open polls/ file and import the new models and register them under admin

from django.contrib import admin

from .models import Question, Choice

Now we can manage questions and answers using the admin in a very basic way but for instance, we cannot see the choices under each question and this is not what we want

Creating and Showing a Custom Model In Admin Aria

You can create a custom admin model view to show in admin area by combining two models when they are related, in the example below we create and we register the question model as a TabularInline then we attach it to the custom Question model to show the choices (answers) of each question under the question itself

from django.contrib import admin

from .models import Question, Choice

class ChoiceInline(admin.TabularInline):
    model = Choice
    extra = 3

class QuestionAdmin(admin.ModelAdmin):
    fieldsets = [(None, {'fields': ['question_text']}),
    ('Data Information', {'fields': ['pub_date'], 'classes': ['collapse']}),]

    inlines = [ChoiceInline], QuestionAdmin)

Setting the Admin Area Site’s Name, Title, Header

Add these at bottom of of your main application, make sure that admin is imported "Pollster Admin" "Pollster Admin Area" "Welcome to the Pollster Admin area"