π Django Step-by-Step Learning Repository Welcome to the ultimate guide for mastering Django! This repository is designed to take you through Django's intricacies step by step, making the learning process smooth and enjoyable.
- Create a folder(anywhere you want)
- open folder in vscode editor
- open terminal
- from right side of the terminal select command prompt ( by default it selected with powershell)
- make a vertual environment :
python -m venv venv
- Activate virtual environment :
venv\Scripts\activate
- Install Django :
pip install django
- Create a django project :
django-admin startproject app
- Run project
- Enter your project & run :
python manage.py runserver
- you can see your project run over :
http://127.0.0.1:8000/
- copy url and hit browser & enjoy your first Django run
- Enter your project & run :
- Create a app inside project :
django-admin startapp app
- Configure project
settings.py
to configure our newapp
- Go to
settings.py
. Then insideINSTALLED_APPS = []
list, add'app',
which is your created appName - Create a
templates
foder inside project directory. Whereapp,manage.py,templaes
ar in same lavel - Go to
settings.py
. Then nnsideTEMPLATES = []
list, add'DIRS': ['templates']
- Inside templates folder create a
index.html
file & just write something, like Hello world in html file<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Django App</title> </head> <body> <h1>Hello world</h1> </body> </html>
- Now to connect app
urls.py
, with main projecturls.py
, createurls.py
insideapp
from django.urls import path urlpatterns = [ ]
- Go to app
views.py
& create a functionindex
def index(request): return render(request,'index.html')
- Go to again app
urls.py
& includeviews index function
for specific urlfrom app import views from django.urls import path,include urlpatterns = [ path('',include(views.index,name="index")) ]
- Go to project
urls.py
& includeapp urls.py
filefrom django.contrib import admin from django.urls import path,include urlpatterns = [ path('admin/', admin.site.urls), path('',include('app.urls')) ]
- Close & rerun server again. Your index.html file should see in the browser successfully
- Go to
- Create seperate app(Auth) inside project :
django-admin startapp Auth
. Make sure yourvenv
is activated
- install Xampp
- Go to project
settings.py
and make database configuration as like bellow
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME' : 'travel', # Database name
'USER' : 'root', # Database user name
'PASSWORD': '', # Database password
'HOST': 'localhost',
'PORT': '3306' # Mysql port number
}
}
-
Run :
pip install mysqlclient
This command confirm your database settings -
Go to project & run :
python manage.py migrate
. This command create, default django table inside database. -
Create models to create custom table inside database.You should register models inside
app/admin.py
. Also add some settings insettings.py
. Then run 2 commands :python manage.py makemigrations
,python manage.py migrate