Tutorial 4: Writing first Django App (Part 2)

Tutorial used: https://docs.djangoproject.com/en/dev/intro/tutorial02/
What is covered:
1) Activating Admin Site
2) Making App modifiable in Admin (and customizing admin form)
3) Customize Admin look and Feel (a.k.a admin templating)

What I learnt:

Concept 1: Admin is boring
Screen Shot 2012-12-28 at 6.16.38 PM

Concept 2: Activating Admin Site
Screen Shot 2012-12-28 at 6.19.30 PM

Note: urls.py maps URL roots to applications
Note: adding admin causes an error 404 – because i have not added any views, nothing is being retrieved

Concept 3: Make your App modifiable in admin (and customizing admin form)
Screen Shot 2012-12-28 at 6.31.37 PM

Note: Modifying a file in django causes server to auto-reload code but Creating a new file does not trigger the auto-reload
Note: This is super awesome! All the widgets and the interface in beautiful.

We can also customize the admin form:
Screen Shot 2012-12-28 at 6.40.15 PM

Note: creating a model admin object and pass it as a second argument allows you to set the order of admin options. So this will cause ‘pub_date’ to be displayed before ‘question’

We can also split form into blocks of fieldsets:
Screen Shot 2012-12-28 at 6.44.37 PM

To Add related objects:
Screen Shot 2012-12-28 at 6.48.20 PM Screen Shot 2012-12-28 at 6.50.07 PMScreen Shot 2012-12-28 at 6.48.29 PM

Note: this tells django that “Choice objects are edited on the Poll admin page. By default, provide enough fields for 3 choices.”
Note: instead of “StackedInline” use “TabularInline” to save viewing space

To change the way objects are displayed:

Screen Shot 2012-12-28 at 6.56.53 PM

Add a list_filter, search_fields, date_heirarchy to poll admin (PollAdmin):
Screen Shot 2012-12-28 at 7.06.30 PM Screen Shot 2012-12-28 at 7.06.37 PM Screen Shot 2012-12-28 at 7.06.43 PM

Concept 4: Customize admin look and feel
Screen Shot 2012-12-28 at 7.10.57 PM

Note: next we will copy the template “admin/base_site.html” from Django admin template directory. It is located in “/Library/Python/2.7/site-packages/django/contrib/admin/templates/admin/base_site.html”. Copy that file to “mytemplates/admin” directory that you just created.

Note: need to create “admin folder” within “mytemplates”

Note: any of Django’s default admin templates can be overridden. To override a template, just do the same thing you did with base_site.html — copy it from the default directory into your custom directory, and make change



This entry was posted in Tutorials. Bookmark the permalink.

One Response to Tutorial 4: Writing first Django App (Part 2)

  1. Pingback: Snippet 4: Adding inline to admin for both ForeignKey and ManyToManyField | django unchain

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s