python --version
pip --version
pip install virtualenv
mkdir Website
cd Website
python -m venv backend
cd backend
Scripts\activate
pip install django
pip install -U Django
py -m pip install --upgrade pip
django-admin startproject backend .
python manage.py startapp pages
python manage.py startapp products
python manage.py migrate
python manage.py createsuperuser
python manage.py runserver
python manage.py runserver 8001
python manage.py runserver 0.0.0.0:8000
http://127.0.0.1:8000/
http://127.0.0.1:8000/admin/
INSTALLED_APPS = [
'pages.apps.PagesConfig',
'products.apps.ProductsConfig',
# . . .
]
from django.contrib import admin
from django.urls import path
from django.urls import include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('pages.urls')),
path('', include('products.urls')),
]
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
from django.urls import path
from . import views
urlpatterns = [
path('products', views.products, name='products'),
path('product', views.product, name='product'),
]
from django.http import HttpResponse
Create your views here.
def index(request):
return HttpResponse("Hello, world. You're at the index page.")
from django.shortcuts import render
def index(request):
return render(request, 'pages/index.html')
from django.shortcuts import render
def index(request):
return render(request, 'pages/index.html', {'name': 'Hossam Rashad'})
from django.shortcuts import render
def index(request):
return render(request, 'pages/index.html', {'name': 'Hossam Rashad', 'age': 30})
from django.shortcuts import render
def index(request):
x = {'name': 'Hossam Rashad', 'age': 20}
return render(request, 'pages/index.html', x)
from django.shortcuts import render
def index(request):
x = {
"empty": "",
"name": "Hossam Rashad",
"title": "Home Page",
"age": 20,
"list": [1, 2, 3],
"dict": {"a": 1, "b": 2},
"none": None,
"bool": True,
"bool2": False,
"fileSize":16154544889,
"fileSize1":54645758,
"fileSize2":111000,
"fileSize3":503
}
return render(request, 'pages/index.html', x)
E:Website\backend\backend\
📁 backend
📁 pages
📁 products
📁 templates
📁 pages
└── 📝 index.html
📁 products
└── 📝 products.html
└── 📝 product.html
templates
pages
index.html
templates\pages\index.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h1>Hello World!</h1>
<h2>Home Page</h2>
<p>Welcome to my first Django project!</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Name = {{name}}</h2>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Name = {{name}}</h2>
<h2>Age = {{age}}</h2>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Name = {{name}}</h2>
<h2>Age = {{age}}</h2>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>If No Data = {{empty|default:"Nothing Found"}}</h2>
<h2>String Slice = {{title|slice:5}}</h2>
<h2>String Slice = {{title|slice:'0:5'}}</h2>
<h2>Add String Or Value = {{title|add:" Index"}}</h2>
<h2>Add String Or Value = {{title|add:" Index"}}</h2>
<h2>String Length = {{title|length}}</h2>
<h2>String Cut = {{title|cut:' '}}</h2>
<h2>String Cut = {{title|cut:'o'}}</h2>
<h2>String To Lower Case = {{title|lower}}</h2>
<h2>String First To Upper Case = {{title|capfirst}}</h2>
<h2>String To Upper Case = {{title|upper}}</h2>
<h2>age = {{age}}</h2>
<h2>File Size = {{fileSize|filesizeformat}}</h2>
<h2>File Size = {{fileSize1|filesizeformat}}</h2>
<h2>File Size = {{fileSize2|filesizeformat}}</h2>
<h2>File Size = {{fileSize3|filesizeformat}}</h2>
</body>
</html>
- Extends
Page Base
<style>
body {
background: #000
}
</style>
Page Index
{% extends 'base/base.html' %}
- Extends
- Block
- Include
- If
- For
- Url
import os
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'templates')],
# ...
},
]