How to Django — Setting up

Building your first Django website

Emily Y Leung
Geek Culture
4 min readAug 19, 2021

--

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:

  1. Installing Django
  2. Commands for setting up and running the website
  3. Folder Structure
  4. Creating a Django app
  5. 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
  • Verify that Django can be seen by Python. To do this, run Python in your Command Prompt

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)
  • Navigate inside the Django project folder

3 — Folder Structure

  • All new websites will have the same default file structure

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)
  • The structure will update to the following

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

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:
  • Add the following (that has been bolded)
  • Open the file sitename > sitename > appname > views.py
  • Create the file sitename > sitename > appname > urls.py and add the following:

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):

  • Save your files and let’s run the server
  • Navigate to 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

--

--