Part 2: The admin site
====> Creating an admin user
$ python manage.py createsuperuser
Username: admin
Email address: admin@example.com
Password: **********
Password (again): *********
Superuser created successfully.
====> Start the development server
$ python manage.py runserver
====> Enter the admin site
(http://127.0.0.1:8000/admin/)
====> Make the poll app modifiable in the admin
$ edit pollsadmin.py
from django.contrib import admin
from .models import Question
admin.site.register(Question)
====> Explore the free admin functionality
====> Customize the admin form
$ edit pollsadmin.py
from django.contrib import admin
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
fields = ['pub_date', 'question_text']
admin.site.register(Question, QuestionAdmin)
====> Split the form up into fieldsets
$ edit pollsadmin.py
from django.contrib import admin
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date']}),
]
admin.site.register(Question, QuestionAdmin)
====> Assign arbitrary HTML classes to each fieldset
$ edit pollsadmin.py
from django.contrib import admin
from .models import Question
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
admin.site.register(Question, QuestionAdmin)
====> Adding related objects
$ edit pollsadmin.py
from django.contrib import admin
from .models import Choice, Question
# ...
admin.site.register(Choice)
====> Remove the register() call for the Choice model. Then, change the Question registration code
$ edit pollsadmin.py
from django.contrib import admin
from .models import Choice, Question
class ChoiceInline(admin.StackedInline):
model = Choice
extra = 3
class QuestionAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question_text']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
inlines = [ChoiceInline]
admin.site.register(Question, QuestionAdmin)
====> Change the display style of the related objects in a more compact, table-based format
$ edit pollsadmin.py
class ChoiceInline(admin.TabularInline):
#...
====> Customize the admin change list -- use the list_display admin option
$ edit pollsadmin.py
class QuestionAdmin(admin.ModelAdmin):
# ...
list_display = ('question_text', 'pub_date', 'was_published_recently')
====> Add a “Filter” sidebar -- using the list_filter admin option
$ edit pollsadmin.py
class QuestionAdmin(admin.ModelAdmin):
# ...
list_filter = ['pub_date']
====> Add some search capability
$ edit pollsadmin.py
class QuestionAdmin(admin.ModelAdmin):
# ...
search_fields = ['question_text']
====> Customize the admin look and feel
====> Customizing your project’s templates
$ mkdir templates
$ edit mysitesettings.py
TEMPLATES = [
{
# ...
'DIRS': [os.path.join(BASE_DIR, 'templates')],
# ...
},
]
$ mkdir templatesadmin
$ copy C:python34libsite-p~1djangocontribadmin emplatesamdinase_site.html templatesadminase_site.html
$ edit tempaltesadminase_site.html
# replace {{ site_header|default:_('Django administration') }} with Polls Administration
# ...
{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">Polls Administration</a></h1>
{% endblock %}
# ...
====> Customizing your application’s templates
====> Customize the admin index page