startproject
command and the general concepts of apps. But, actually, Django provides incredible flexibility if desired around structure.urls.py
file is the only 'mandatory' part of the exercise.five
and make a new Django project. We'll be using Pipenv here and calling our top-level directory config
.pages
. Make sure you've stopped the local server by typing Control+c
.INSTALLED_APPS
setting.config/urls.py
file by importing include
and adding a path for the pages app.urls.py
file within our app, a views file, and a template file. pages/urls.py
file. First, create it by using the touch
command if you're on a Mac.home1/
, a HomePageView
we haven't created yet, and the URL name of home
.TemplateView
and points to a template called home.html
.templates
directory within the pages
app, then a new directory called pages
, and finally the template called home.html
.python manage.py runserver
and navigate to http://127.0.0.1:8000/home1/.pages
app for static pages. But since this article is for educational purposes, let's see 4 more ways we can achieve the same outcome.TemplateView
for a function-based view. Django allows for either using function-based or class-based views.render
at the top of pages/views.py
and add a new view, home_page_view2
, which requests the same template home.html
.home2/
, in the pages/urls.py
file.HttpResponse
and hard-coding the string Hello World
to be returned.config
directory.TemplateView
at the top, create a new URL path, pointed to the existed template within the path, and provided an updated URL name of home4
.models.py
file and leveraging the built-in DetailView.pages/models.py
. We'll only add a single field, text
, to the model called Post
. Adding an optional __str__
method means the model will display nicely within the Django admin.migrate
to update the database.pages/admin.py
file to see our pages
app within the admin so update pages/admin.py
as follows:Posts
under Pages
.pages/urls.py
where we'll import a new view called HomeDetailView
and add a path for it.pages/views.py
file needs to import DetailView
at the top and our Post
model. Then create HomeDetailView
which references our Post
model and a new pages/home5.html
template.home5.html
that will display the text
field from our model.python manage.py runserver
and navigate to http://127.0.0.1:8000/home/1/ to see our 5th approach in action.