To keep everything tidy, we will create a separate application inside our project, this will help us to have some nice solid foundations as the project expands. To create an application we need to run the following command in the console (from bakery_site
directory where manage.py
file is):
macOS or Linux:
(myvenv) bakery_site% python manage.py startapp bakeries
Windows:
(myvenv) C:\Users\Name\bakery_site> python manage.py startapp bakeries
You will notice that a new bakeries
directory is created and it contains a number of files now. The directories and files in our project should look like this:
bakery_site
βββ bakeries
β βββ admin.py
β βββ apps.py
β βββ __init__.py
β βββ migrations
β β βββ __init__.py
β βββ models.py
β βββ tests.py
β βββ views.py
βββ db.sqlite3
βββ manage.py
βββ bakery_project
β βββ asgi.py
β βββ __init__.py
β βββ settings.py
β βββ urls.py
β βββ wsgi.py
βββ myvenv
β βββ ...
βββ requirements.txt
Congratulations - you have an app now!
Next, we want to set it up so that when someone visits the homepage (http://127.0.0.1:8000/), Django knows to run a specific function called a view.
The view is the brain behind what your user sees on the screen. It receives the request, adds any extra logic weβve added (like filtering, sorting, etc.) and pass it to a template
. Weβll create a template a bit later on.
We will add our views to the bakeries/views.py
file.
OK, letβs open up this file in our code editor and see whatβs in there:
from django.shortcuts import render
# Create your views here.
Not too much stuff here yet.
Remember that lines starting with #
are comments β this means that those lines wonβt be run by Python.
Letβs create a view as the comment suggests.
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. Welcome to our bakery.")
Make sure you hit save on that file and we will go on to URLs.
A URL is a web address. You can see a URL every time you visit a website. (127.0.0.1:8000
is a URL! And https://shecodes.com.au/
is also a URL.)
Every page on the Internet needs its own URL. Remember that letters analogy way back at the start? The URL is like your address, so the post office knows where to send your mail. Now imagine you live in an apartment building, you might need a way to differentiate your apartment from Bobβs next door. In addresses that might be an apartment number or unit number within a building. In a URL it might be something like shecodes.com.au/mentor vs shecodes.com.au/shop - same building but different apartments.
This way your application knows what it should show to a user who opens that URL. In Django, we use something called URLconf
(URL configuration), which is a set of patterns that Django will try to match the requested URL to find the correct view.
Letβs open up the bakery_project/urls.py
file in your editor and take a look.
When you create a new Django project, Django automatically creates this file for you:
"""mysite URL Configuration
[...]
"""
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
Time to create our first URL! We want βhttp://127.0.0.1:8000/β to be the home page of our app and to display a list of bakeries.
In your bakery_project/urls.py
file please add the following lines under from django.contrib import admin
:
from django.urls import path, include
from bakeries.views import index
urlpatterns = [
path('admin/', admin.site.urls),
path('', index),
]
The include()
function lets you include other URL configurations from other apps.
Django will run the index
view whenever someone goes to the homepage URL (which is just an empty string after the domain name, like http://127.0.0.1:8000/
).
Alright, save that file. VS Code is super helpful and will have a dot next to your file name if youβve got unsaved changes. Its really easy to forget to save, so if youβre running code and its not working as expected, double check youβve saved the files.
Lets check to see if our website is saying hello to us. As a reminder, you need to be in the directory that contains the manage.py
file (the bakery_site
directory). In the console, we can start the web server by running python manage.py runserver
:
(myvenv) bakery_site% python manage.py runserver
Since weβve run this command before we can actually do a nifty little shortcut. In our terminal, press the up arrow and it should cycle through commands youβve previously typed. This will be helpful if youβre running the same commands over and over again to just press the up arrow and enter rather than typing it all out. Remember, developers love typing as few keystrokes as possible.