Overview
Welcome to Quarter 4!
This week will leave our work with the Django polls tutorial and resume our work in Dr. Chuck's Django for Everybody.
Wednesday, April 17th and Friday, April 19th
Classwork / Homework
I will be away with our high school programming team at the High School Programming Contest on Friday, so we need to plan today for both today and Friday.
Complete each of the activities described below. Take notes on all these
activites in a markdown file named URLsDispatch.md
(which you
should link to from your README.md
).
Be ready for a short quiz on what you have learned at the beginning of class on Tuesday.
Add a Homepage to Your Django Projects
Let's begin by adding a homepage to your django_projects
site.
We will be using the Option 2: Project Level approach as discussed in
Django Best
Practices: Template Structure.
Here's what you need to do:
- Create a new
templates
directory in the outer of your twomysite
directories (the one withmanage.py
and your database file in it). - Modify the
TEMPLATES
list insettings.py
as discussed in Option 2. - In
mysite/urls.py
add this import:from django.views.generic import TemplateView
and this urlpattern:path('', TemplateView.as_view(template_name="home.html")),
- Create a homepage in a file named
home.html
in the newtemplates
directory you made in the first step. - Include an unordered list in this homepage with a heading,
My Django Apps
, and a link to"polls/"
which should bring the viewer to your polls app when clicked.
How and Where Does DJ4E Samples Do This?
Having successfully created a home page for your django_projects
,
let's take a look into the files of gctaa_dj4e_samples
to see how
and where it does the same thing we just did.
By the time you come to class next Tuesday, be ready to answer the following questions:
- Which subdirectory of
gctaa_dj4e_samples
is the main project directory (the equivalent of the innermysite
subdirectory in ourdjango_projects
)? How can you tell? - When you run the server in
gctaa_dj4e_samples
, which template file is being used? How do you know? - Is the setup for the
gctaa_dj4e_samples
main page the same as the one we just used above? Why or why not?
URL Dispatching in Django
Watch the video URL Routing in Django (Note: I've now watched it several times, and I highly recommend doing that if you want to learn Django). Then take another look at Dr. Chuck's Django components diagram:
When you finish, make sure you can answer the following questions:
- What is the name for official name for the Django process that Dr. Chuck calls Routing in his diagram (Note: he specifically mentions this in the video)? Which two files (the gray boxes in the diagram) are connected through this process?
- Web applications often follow a design pattern called Model-View-Controller. Django implements its own variation on this pattern. Read MVC pattern in Django. Which Django components are used to implement each of the MVC components?
Since I've just been told this URL is blocked I've made a PDF here.