How to Django — Setting up
Building your first Django website
Prerequisites
Before you jump into building websites using Python and Django, make sure you have a grasp of Virtual Environments.
Process of building a Django Website:
- Installing Django
- Commands for setting up and running the website
- Folder Structure
- Creating a Django app
- Hello World
1 — Installing Django
To get into building with Django, we first need to install this package.
- Open Command Prompt
- Navigate and activate your virtual environment
- Pip install Django
pip install Django
- Verify that Django can be seen by Python. To do this, run Python in your Command Prompt
python>>> import django>>> print(django.get_version())
2.1.2 <-- This is what I am using at the moment.
If you are having problems and cannot seem to get a version. Check the Django docs.
2 — Commands for setting up and running the website
Now that we have Django installed, we can create our first website.
- Navigate to your project folder (where the project will sit)
- Create a new Django project using the following command (no spaces allowed in name)
django-admin startproject sitename
- Navigate inside the Django project folder
cd sitename
3 — Folder Structure
- All new websites will have the same default file structure
sitename/
manage.py
sitename/
__init__.py
settings.py
urls.py
wsgi.py
The following has been referenced from here:
__init__.py is an empty file that instructs Python to treat this directory as a Python package.
settings.py contains all the website settings. This is where we register any applications we create, the location of our static files, database configuration details, etc.
urls.py defines the site url-to-view mappings. While this could contain all the url mapping code, it is more common to delegate some of the mapping to particular applications.
wsgi.py is used to help your Django application communicate with the web server. You can treat this as boilerplate.
The manage.py script is used to create applications, work with databases, and start the development web server.
4 — Creating an App
Now that the main site has been created, we can add apps to the mix.
- Inside the Django project folder, create a Django app using the following command (again, no spaces in the name)
python manage.py startapp appname
- The structure will update to the following
sitename/
manage.py
sitename/
appname/
admin.py
apps.py
models.py
tests.py
views.py
__init__.py
migrations/
A migrations folder, used to store “migrations” — files that allow you to automatically update your database as you modify your models.
__init__.py — an empty file created here so that Django/Python will recognise the folder as a Python Package and allow you to use its objects within other parts of the project.
- One thing to note is that you will probably have to create another urls.py file everytime you create a new app. I’m not sure why it is not a default file…
5 — Hello World
With the app created, we need to install it into the website so that we can see it and use it.
To do this, open the settings.py
file in: sitename > sitename > settings.py
.
- Scroll down to
INSTALLED_APPS
- Create another entry either at the end or start of the array
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'appname', <-- Install the app here
]
Now that it’s installed, we can add the app as a url pattern for routing to the app.
- Open
sitename > sitename > urls.py
where you will see the following:
from django.contrib import admin
from django.urls import pathurlpatterns = [
path('admin/', admin.site.urls),
]
- Add the following (that has been bolded)
from django.contrib import admin
from django.urls import path, includeurlpatterns = [
path('admin/', admin.site.urls),
path('', include('appname.urls')),
]
- Open the file
sitename > sitename > appname > views.py
from django.http import HttpResponse, JsonResponse# Create your views here.
def home(request):
return HttpResponse('<h1>Hello World</h1>')
- Create the file
sitename > sitename > appname > urls.py
and add the following:
from django.urls import path
from . import viewsapp_name = "appname"urlpatterns = [
path('', views.home, name="home"),
]
What you should take note of is that we are importing the view (‘home’) we created in views.py and referenced it into our URL pattern.
When we want to add a parameter to the route, we can use the following (where the backslash ends the route):
from django.urls import path
from . import viewsapp_name = "main"urlpatterns = [
path('', views.home, name="home"),
path('test/', views.test, name="test"),
]
- Save your files and let’s run the server
python manage.py runserver
- Navigate to localhost:8000
http://localhost:8000
You can also create a JSON response instead, which can be changed in the output of the home definition that lives in sitename > sitename > appname > views.py
from django.http import HttpResponse, JsonResponse# Create your views here.
def home(request):
return JsonResponse({"hello":"world"})