Create a Simple Django Boilerplate (Part 2)
We will continue from where we left off in Part 1 of the tutorial and create a view for the home page.
This was what we achieved in part 1 of the tutorial.
- Set up a virtual environment
- Installed Django
- Saved our dependencies into requirements.txt
- Created a Django project and application
- Applied migrations
We have not written a single line of code yet has already generated a functional starter template. This shows the ease of scaffolding an application in Django.
Activate your application
To get our application working, we need to include it in our project. Open demo/settings.py and add the following to the INSTALLED_APPS setting.
INSTALLED_APPS = [ 'boilerplate.apps.BoilerplateConfig', ... ... ]
Where did we get the BoilerplateConfig reference?
When we created a new application, Django generated an apps.py file with the class BoilerplateConfig.
This class allows us to reference the application to the project.
Creating a basic view
Let's start by writing a simple view. Add the following code to boilerplate/views.py.
from django.http import HttpResponse def index(request): return HttpResponse("Hello World.")
If you start the server, you will see that the home page remains the same. Why?
This is because our demo project does not recognise our new view yet.
In Django, we need to explicitly map views to URL paths. We can do this by creating a URL configuration.
Create a urls.py file in the boilerplate folder and paste the following code.
from django.urls import path from . import views urlpatterns = [ path('', views.index, name='index'), ]
We need to hook up the new URL configuration to our main project. Open demo/urls.py and add the following.
from django.contrib import admin from django.urls import include, path urlpatterns = [ path('', include('boilerplate.urls')), path('admin/', admin.site.urls), ]
If you visit http://localhost:8000 now, you should see the words "Hello World.". Nothing fancy. Let's spice it up using Django's template system.
First, create a templates directory inside your boilerplate directory. Create another directory name boilerplate inside the templates directory.
Next, create an index.html file.
You should end up with a structure like boilerplate/templates/boilerplate/index.html.
Paste the following code into index.html.
<div> <h1>Hello World.</h1> <p>This is my new landing page.</p> </div>
Why this convoluted directory setup?
In Django, applications looks for templates inside a templates subdirectory.
By using template namespacing, Django will render the templates that matches your application name.
Avoid placing all your templates inside name-of-your-project/templates as Django would be unable to differentiate the templates used for different applications.
Add the following code to boilerplate/views.py.
from django.shortcuts import render def index(request): return render(request, 'boilerplate/index.html')
A few things are happening here.
- We import the Django shortcut render() function.
- We specified an index view to render the page using our "Hello World" template.
Start the development server and you should see the new rendered template.
Congratulations! You have written your first view.
In part 3 of the tutorial, we will refactor the code to read records from the database and render the results to the template.
blog comments powered by Disqus