An Introduction to Django – Part 7

This will be our last Introduction to Django post.  Here, we’ll look at customizing the admin site.  Next time, we’ll look at making our own Django app to help organize our grocery shopping list.

Customize The Admin Form

Changing The Order of Fields

Recall from Part 2 that we registered the Question model with admin.site.register(Question) within the polls/admin.py file.  This allowed Django to automatically generate an admin site with a default form.

To customize this page, open the polls/admin.py file and change it to this:

Next, start the development server by navigating to the manage.py file and running the command

Then, go to the admin tool at http://127.0.0.1:8000/admin/ and navigate to change a question.  It should look like this:

Capture

The above change put the date published date before the question text.

Add Related Objects

Our questions can have multiple choices, but the Question admin page doesn’t display them.  We have a couple of ways to solve this.

First, open the polls/admin.py file and register the Choice with the admin just like we did above for Question:

Now, within the admin tool we see an option for Choices and Questions:

Capture

If we navigate within the Admin Tool to Choices – Add Choice we will see the following:

Capture

The dropdown contains all of our questions (for me, this is just “What’s up?”).  We can add one choice at a time to each question.  This is maybe better than how we were doing it before, but it would be much more efficient if we could add all of the choices when we add the actual question.

Within the polls/admin.py file, remove the line

And make the file look like this:

This lets Django know that we want to edit the Choice objects when we edit the Question within the admin page, and it gives us enough room to add 3 choices (this is defined by the extra variable).

Open the Add Question page and it should look like this:

Capture

Note that at the bottom of the Choices, you have the option to click a link to add more.  Pretty cool!

If you don’t like how much room this takes up, you can change the ChoiceInLine class within the polls/admin.py file to be this instead:

This changes the Add Question page to look like this:

Capture

Customizing The Change List

The Change List is the page that shows all of our questions.  It looks like this for me:

Capture

Maybe we want to see more information about each question such as the date published and if it was published recently.

Open the polls/admin.py file and add the following:

This will make the page look like this:

Capture

Django will automatically let you click on any of the columns to sort by that column.  We can change the names of the columns by adding attributes to the Question method within polls/models.py.  Open that file and add the following:

Additionally, edit the admin.py file and add:

This will give us a filtering option.  Reload the Change List page and it should look like this:

Capture

Note that we only told it to filter by the publication date.  Django automatically knew that this datatype was a date type and added ‘Any Date’, ‘Today’, ‘Past 7 days’, etc.

Add a search field by adding the following to the QuestionAdmin class:

This will add a search bar near the top:

Capture

Customize the Admin Look and Feel

Customize Project Templates

Navigate to the project directory (where your manage.py file is located) and add a templates directory there.  For me, this is pantrio/templates.

Next, open your mysite/settings.py file.  For me, this is pantrio/pantrio/settings.py.  Look for the TEMPLATES variable (for me, it is on line 56) and add the following to the ‘DIRS’ option (for me, line 59):

Next, we need to figure out where our Django source code is so that we can copy a template from it.  Go to the command prompt and type:

This will output where your Django lives.  For me, it is C:/Python27/Lib/site-packages/django

Navigate to this directory.  Then, go to contrib/admin/templates/admin and copy the base_site.html file.  Paste this file into your own app/templates/admin directory (for me this is pantrio/templates/admin).

Open this file and replace

with the site’s name.  My base_site.html file looks like this:

This changes our Admin Pages from saying ‘Django Administration’, like this:

Capture

To whatever you set it to.  Like this:

Capture

 

That’s all for the Introduction to Django series.  I still feel like there are many things I don’t quite understand, but I am excited to try to jump in and make my own app!

Have questions or suggestions?  Please feel free to comment below or contact me.

Leave a Reply

Your email address will not be published. Required fields are marked *