Setting Django Admin Area Django.How

Author avatar wrote on 26/05/2022

  1. Type python manage.py createsuperuser

  2. Enter the preferred username, email, password

  1. Run the server python manage.py runserver

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

Allowing Managing Questions and Answers Using Admin Panel

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


from django.contrib import admin

from .models import Question, Choice

admin.site.register(Question)
admin.site.register(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]

admin.site.register(Question, QuestionAdmin)

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

Add these at bottom of url.py of your main application, make sure that admin is imported


admin.site.site_header= "Pollster Admin"
admin.site.site_title= "Pollster Admin Area"
admin.site.index_title= "Welcome to the Pollster Admin area"
 
CRUDgen Django CRUD generator