In this article, I’ll cover how to create a simple Django project with a homepage. I assume you already have Django installed. Read this article to learn how to get Django.
Start a new project
To start a new project:
django-admin startproject home
This will create a folder called home
on your computer. Next, check that everything installed correctly by activating the Django Server:
python manage.py runserver
Doing this starts the development server. Visit http://127.0.0.1:8000/
in your browser. If Django installed successfully, you’ll see the Django Welcome Page. Keep this server running as we will need it later.
Setup the templates
In Django, templates are the HTML files that determine the structure of your webpages and how they look. Create a folder called templates
in the top or root level directory of your project. Open templates
and create a file in it and name it anything you like. I’ll call it homepage.html
. For the purposes of this post, I’ll just write one line of HTML in it. Next, open homepage.html and add the following line of code to it:
<h1>Hello there! I'm a Django website</h1>
This adds a simple header to the page. Now that you’ve created a template, the next thing is to tell Django where to find it. Find the settings.py
file and open it in an editor. You’ll want to make a one line change to the setting 'DIRS'
under 'TEMPLATES'
TEMPLATES = [ { ... 'DIRS': ['templates'], ... } ]
The next step is to configure a View and a URL Configuration(URLConf for short) for the homepage. Every page in a website should have a URL. The URL makes it easy to find the page. In Django, a view determines what gets displayed on a page and the URLConf matches the URL it receives to the right view and template. You can say that the URLConf looks up the right view to display to the user.
Django supports a couple of ways to define views. I’ll show you the Function Based Views and the Class Based Views.
Function Based View
Open up views.py
and add the following to it:
from django.shortcuts import render def home_page_view(request): return render(request, 'homepage.html')
This view uses a function home_page_view()
to render homepage.html
. This means that whenever home_page_view()
is called, the contents of homepage.html
will be displayed.
The next step is to make changes to the URLConf to point it to this view. Open the urls.py
file in your editor and add the following:
from django.urls import path from .views import home_page_view urlpatterns = [ path('', home_page_view, name='home'), ]
Whenever a user requests the homepage, represented by the empty string above, the home_page_view()
function gets called.
Test that everything works by navigating to http://127.0.0.1:8000/. You should see the “Hello there! I’m a Django website” text from the homepage page we created in the first step.
Class Based View
The second way to define the same view is to use a class instead of a function. Open up views.py
and add the following to it:
from django.views.generic import TemplateView class HomePageView(TemplateView): template_name = 'homepage.html'
Next make these changes to urls.py
:
from django.urls import path from .views import HomePageView urlpatterns = [ path('', HomePageView.as_view(), name='home'), ]
Here you import the HomePageView
class based view from the views.py
file. HomePageView gets called whenever the user requests the homepage.
Navigating to http://127.0.0.1:8000/ should display the same “Hello there! I’m a Django website” text as before.
<
h3>Conclusion
You’ve seen how to setup a Django site that has a single static page. You also learned about the Function Based Views as well as the Class Based Views. Thanks for reading.