Writing your First Django App (The Beginner’s Guide)
By: Onlygod Ovbije
Tags: Beginners Guide, Django Framework, Django, Template, Python

In this tutorial✌, I will be sharing quick steps to get started as a beginner using the Django framework.

Software and Tools

In this article I am using WSL terminal in Windows for OS, you can also use others like mac, linux, if any commands are different, you can search Google to resolve it.

Let’s get started !!

1. Create directory, and virtual environment

Create a project directory, in this case I use “django-project”

mkdir django-project

Point to django-project directory

cd django-project

Build virtual environment for the directory using pipenv

pipenv install shell

this will create a virtual environment and also activate it.

Image of Complete process (Virtual Environment)

2. Installing Django

After complete creating the directory and virtual environment, it’s time to install Django

pip install django

Installing django

To see what you have installed it

pip freeze

Viewing installed packages

3. Open desired IDE or Text Editor

Open an IDE or text editor you love or get familiar with, e.g. PyCharm, VisualStudio Code, SublimeTex, etc. I will be using vscode.

To quickly open your directory on vscode using the terminal just type in

code .

this will automatically open vscode in your directory mine is (django-project), also you can just manually open it.

Vscode directory view

4. Start Django project

Now it’s time to create our first project so the project will have only one project in our project directory, so we can add apps to it.

Back to terminal, simply type in

django-admin startproject django_project

I named my django project: (django_project) you can change yours.

Now back to visual studio code you will see this

your django project

To check how everything works now, from your terminal

cd django_project python manage.py runserver

Running the django server

visit http://127.0.0.1:8000/using your browser

Great it works 👌

5. Now Starting your Django app

from your terminal, stop the running server using CTRL + C, then type in this to create a django app

python manage.py startapp django_app

If Starting Django App

I named mine (django_app) you can change yours

if successfully done you can see this

Django App Directory

5. Registering your Django app

To let django know you have added a new app. simply go to setting.py in your django project directory (djang_project) then scroll to installed apps

Register your app

6. Using templates in your Django app

To use templates in your Django app simply create a templates folder in your app directory ( django_app/templates/ ) then add a new directory with the name off your app the end result will look like this ( django_app/templates/django_app/ )

) then add a new directory with the name off your app the end result will look like this ( ) now lastly create a new html file in it index.html, your final directory will look like this (django_app/templates/django_app/index.html)

add templates to app

6. Connecting the views and urls for your app

Now we are done in creating the template next is to connect it for viewing and routing

So first in your directory go to django_app/views.py then add the following

from django.shortcuts import render from django.views.generic import TemplateView

# Create your views here. class HomePage(TemplateView):

template_name = "django_app/index.html"

Now go to root(django_project) directory django_project/urls.py and add this

from django.contrib import admin from django.urls import path # add this from django_app.views import HomePage

urlpatterns = [ path('admin/', admin.site.urls), # add new url to your app path('', HomePage.as_view(), name='hone'), ]

Now you are set, go back to the template directory

django_app/templates/django_app/index.html

then add this

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My Django App</title> </head> <body> <h1> Hello there, Django App Tempate Works </h1> </body> </html>

Now you are all setup up, To test it out go your terminal of our django project name directory (django_project) as seen below

app running

Now visit http://127.0.0.1:8000/using your browser, you will see this

Viewing your templates

Congrats 🙌🐱‍🏍🐱‍🏍🐱‍🏍🐱‍🏍 your Django app with templates works

Now you are ready to go!! for a quick start check out Django Models