Skip to content
Home » Blog » How to Start a Django Project Now- Complete Guide

How to Start a Django Project Now- Complete Guide

Starting a Project in Django

In this article we will be discussing how to start a Django Project right from Scratch. First of all, Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

We start off by installing Django using Pythons Package Manager and then move on to making other basic installations such as SQLite. We then create an App for our Django Project and define it in settings.py.

And then we migrate our site’s data to a SQLite Database

Lastly, we give our Django Project Views and URLs and a Template and end the post by running our Webserver.

Now that we know what exactly we’re doing, let’s look at what you need to get this working.

Before You Start…

Before we jump in, you would have to have some knowledge on

  • Python
  • Basic Bash Commands such as cd, ls, mkdir
  • Basic HTML

If you are not very familiar with Python, you can check out all our posts related to Python 3.9 here.

For HTML, use this link

Software Requirements

Some of the softwares you need to have installed for this course

  • Python 3.9
  • Pip 3
  • Visual Studio Code (Or any Text Editor for that sake)

If you don’t have python installed type in sudo apt install python3

For installing pip3 type in sudo apt install pip3

Setup

Today, we will be installing all these…

  • Django
  • Git
  • SQLite

Now that you are ready, let’s jump right in

Installations to Start a Project in Django

Django

You can easily install Django by using python’s package manager (pip)

Open your Bash, and type in the following command

pip3 install django
pip install django

This starts the Django installation

Git

You can easily install Django by using python’s package manager (pip)

Open your Bash, and enter this command to check if you have Git installed

git --version
git version 2.28.0

If you get any error, this means you don’t have git installed

Windows

Download the latest Git for Windows installer.

Linux and macOS

Run this command to install Git

sudo apt install git

SQLite

We use SQLite Browser to create a Database and migrate Data for our Django App

Linux and macOS

sudo apt-get install sqlite3

Windows

You can install SQLite from SQLite’s Download Page

Starting a Project in Django

To Create a Django project you must use the following command

django-admin startproject mysite

Where mysite will be the name of your Django Project

When you type ls, you will see a folder named mysite

Navigate into the folder running the command

cd mysite

Open it in your text editor

code .

Here you will see all the files created by Django

Starting a Django App

Go back to your bash and type this command to create a new Django app, you must do it inside the mysite Directory, where the python file manage.py is present

python3 manage.py startapp myapp

Now come back to your text editor, you will see a new folder named myapp where you will have certain python files and we are going to edit these

Starting a Project in Django

Now go to settings.py file under mysite

Here, scroll down until you find a List name INSTALLED_APPS

Starting a Project in Django

Type in the name of your app, in this case myapp

Migration

Type in the following command to show the list of files that are not migrated

python3 manage.py showmigrations
Starting a Project in Django

In order to migrate all these, use this command

python3 manage.py migrate
Starting a Project in Django

Now all of these models would be migrated

Views, URLs, and Templates

URLs

Go to your urls.py file

First you need to import myapp.views, and then create a url path for your project, that points to the index function in views.py

Starting a Project in Django

The Code for this will be,

import myapp.views

urlpatterns = [
    '''
    All Other Paths
    '''
    path('myapp/', myapp.views.index)

In order for this to work we must have an index function inside views.py file

Views

Now go to your views.py file and create the index view functions

Starting a Project in Django

Here we import render function, the define a new function named index, the view that will appear when navigating to the URL myapp/

This function will take in request, and then return a HTML Template, in this case, lets name the template index.html.

The code will look something like this

from django.shortcuts import render

def index(request):
    return render(request, "myapp/index.html")

Now we have created out views

Templates

Remember we referred a template named index.html, well, lets actually create a html file named index.html

To store all the templates, create a folder named templates, and inside that, another folder named myapp

How to Start a Django Project

Here create a new html file named index.html

Type in your html

How to Start a Django Project

It can be anything, the HTML you type in will show up in the URL myapp/

For instance,

<!DOCTYPE html>
<html>
    <head>
        <title>My App</title>
    </head>
    <body>
        <h1>Welcome to my App</h1>
    </body>
</html>

Congratulations! You have created your very own Django Webapp

Lets run it!

Why Create a Folder with App name inside Templates Directory?

Creating a folder named myapp inside the templates folder is completely optional if you had only 1 app inside your Django Project

The purpose of doing this is to avoid confusion if we had multiple apps, where we usually have multiple index.html files

Now since the html file is inside a folder, we can link it using the appname/index.html

Run the Web Server

In order to run your Web Server, type in the following command

python3 manage.py runserver

Now Click on the displayed link

How to Start a Django Project

Now you will be seeing your Django App on your Browser

How to Start a Django Project

To Sum Up…

Today we discussed how to start a Django Project right from Scratch. We started off by installing Django using Pythons Package Manager and then moved on to making other basic installations such as Git and SQLite. And then we created an App for our Django Project, defined it in settings.py, and migrated our Data into our SQLite Database. We ended the post by giving our Django Project Views and URLs and a Template.

If anything didn’t go the way it should, you got some errors and so couldn’t move ahead, feel free that Comment Down below, or make use of our Forums

If you are on your way to becoming a Django Developer, you might find our site really helpful. Hit this link for more Django related posts
Check it out, it’s Simply the Best Code.

Leave a Reply

Your email address will not be published. Required fields are marked *