Set up Django on Windows

Every Django app file structure will look like this.

  • folder_name: a place to set up Django environment
  • project_name: a collection of configuration files
  • app_name: a web application

You will realize the file structure once you successfully follow the instructions below. First, we create a folder named, folder_name. In this folder, we will set up Django environment. Here the app will be connected with MySQL also. Let’s see the steps below.

Setup Python environment

Install pip (a package manager for python)

Install Xampp (for MySQL)

Create Folder

This will create a folder (named folder_name) in directory_path. Django will be installed and activated in that folder.

cd directory_path
py -m venv folder_name
folder_name\Scripts\activate.bat
py -m pip install Django
python -m pip install --upgrade pip

Create Project

Now we will create a project folder (named project_name) in the previous one. We need to provide username, email, and password to create an account for Django dashboard.

cd directory_path
py -m venv folder_name
folder_name\Scripts\activate.bat
cd folder_name
django-admin startproject project_name
cd project_name
py manage.py migrate
py manage.py createsuperuser
py manage.py runserver

Click http://127.0.0.1:8000 to see the server and http://127.0.0.1:8000/admin to login.

Create App

Exit from cmd. Now we have to create Django app, named app_name.

cd directory_path
py -m venv folder_name
folder_name\Scripts\activate.bat
cd folder_name\project_name
py manage.py startapp app_name
py manage.py runserver

Go to folder_name -> project_name -> project_name -> settings.py and add ‘app_name’ string in the object named INSTALLED_APPS. For example,

INSTALLED_APPS = [
.
.
'app_name'
]

Start Server

cd directory_path && py -m venv folder_name && folder_name\Scripts\activate.bat && cd folder_name\project_name && py manage.py runserver

Design Web Page — 1

folder_name -> project_name -> app_name -> views.py

from django.http import HttpResponse
def hello(request):
return HttpResponse("<h2>Hello World</h2>")

Design Web Page — 2

folder_name -> project_name -> project_name -> urls.py

from django.contrib import admin
from django.urls import path
from app_name import views
urlpatterns = [
path('admin/', admin.site.urls),
path('hello/', views.hello),
]

Click http://127.0.0.1:8000/hello to see the result.

Connect Database — 1

Now we want to connect MySQL with our Django app. Open Xampp and start MySQL. Create a database named ‘app_name_database’. Also, you need to collect the username and password of Xampp. You can find it form its config file. Now we have to edit some files from project folder.

folder_name -> project_name -> project_name -> settings.py and edit object named DATABASES.

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'app_name_database',
'USER': 'xampp_username',
'PASSWORD': 'xampp_password',
'HOST': 'localhost',
'PORT': xampp_port_number
}
}

Connect Database — 2

cd directory_path && py -m venv folder_name && folder_name\Scripts\activate.bat && cd folder_name && pip install mysqlclient && pip install pymysql && cd project_name && python manage.py migrate && py manage.py runserver

Create Table — 1

folder_name -> project_name -> app_name -> models.py and type,

from django.db import models
class Student(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=30)
contact = models.IntegerField()
email = models.EmailField(max_length=50)
age = models.IntegerField()

Create Table — 2

cd directory_path && py -m venv folder_name && folder_name\Scripts\activate.bat && cd folder_name/project_name && python manage.py makemigrations app_name && python manage.py migrate && py manage.py runserver

This will make a table in the database. Columns for that table are first_name, last_name, contact, email, and age.

Create Folder, Project and App (shortcut method)

cd directory_path && py -m venv folder_name && folder_name\Scripts\activate.bat && py -m pip install Django && python -m pip install --upgrade pip && cd folder_name && django-admin startproject project_name && cd project_name && py manage.py migrate && py manage.py createsuperuser && py manage.py startapp app_name && py manage.py runserver

--

--

--

CSE student | Problem Solving, Developing | Love to watch movies, series, anime | Reading and Writing Blogs

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Observability, have you ever heard about it?

Test Vpn With Powershell

Boost Your Javascript Knowledge-Part2

CSS GRID: What you need to know to fully exploit its potential

Best Practices For Planning Low-Voltage Cabling

Journal 16 — Starting to Look Like a Game!

Google In Dark Mode

macOS: How to record video with system audio?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nishan Paul

Nishan Paul

CSE student | Problem Solving, Developing | Love to watch movies, series, anime | Reading and Writing Blogs

More from Medium

How to implement dependent dropdown List in django admin

Introduction to Django Framework

introduction to Django Framework

How to create desktop notifications in Django?

Unlocking Django Admin: Part 1