Table of contents
Overview
This article will explain how to download and use the Python Django Framework to create a sample application from the official Django documentation (1).
Materials
Python
Python Packages Needed: Django
Downloading Django
To use Django, it needs to be downloaded:
pip install Django
To upgrade an old version of Django on any computer, type the following command:
pip install django --upgrade
Running Django by First Creating a Project
Go to a new directory and enter the command:
django-admin startproject project_name
The "django-admin startproject project_name" command creates a folder with the name that is specified after startproject. In this tutorial, the folder that contains all the basic files in order to deploy a Django application to production is called "mysite", though, as stated before, it can be named anything.
The following files are made in the chosen directory:
mysite - Another folder within mysite is made that contains the following files:
__init__.py
To run a sample Django application, run:
python manage.py runserver
After running the server, a "db.sqlite3" file is automatically created.
To change the ip or port number for the Django application, it can be specified after runserver
python manage.py runserver 0.0.0.0:8000
Creating an Application in Django
To create an application, write the command in the same directory as the "manage.py" file:
python manage.py startapp app_name
This command to make an application is different from making a project since the word "startapp" is used instead of "startproject". Another folder with the specified app_name will be created.
Editing the views.py File in the Application Folder
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
Making and Editing a urls.py File in the Application Folder
from django.urls import path
from . import views
urlpatterns = [
path("", views.index, name="index"),
]
Editing the urls.py File in the Project Folder
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path("polls/", include("polls.urls")),
path('admin/', admin.site.urls),
]
Sources
https://docs.djangoproject.com/en/5.0/intro/tutorial01/ (1)
Source Code
https://github.com/AndrewDass1/TUTORIALS-AND-NOTES/tree/main/Python/Django/Part%201