文章對應的B站視頻:https://www.bilibili.com/video/BV1Tu41127Ca/
Django系列文章對應的目錄:https://www.cnblogs.com/emanlee/p/15860241.html
Django 官方文檔如下:
https://docs.djangoproject.com/zh-hans/4.0/
https://docs.djangoproject.com/zh-hans/4.0/intro/tutorial01/
安裝Django之后,在Windows的環境變量Path中配置 django-admin.exe 的可以訪問的路徑(C:\Python38\Scripts\)
C:\Python38\Scripts\django-admin.exe
在 D:\temp-test\djangodemo\ 下創建簡單的項目:
在命令行窗口操作:
cd D:\temp-test\djangodemo\
創建項目 mysite
django-admin startproject mysite
啟動項目
python manage.py runserver 8001
瀏覽器里訪問
http://127.0.0.1:8001/
====================================
在 D:\temp-test\djangodemo\ 下創建簡單的項目:
在命令行窗口操作:
cd D:\temp-test\djangodemo\
創建項目 mysite123
django-admin startproject mysite123
啟動項目
python manage.py runserver 8002
瀏覽器里訪問
http://127.0.0.1:8002/
在mysite123文件夾中創建views.py
views.py
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world ! ")
修改 urls.py
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('hello/', views.hello),
]
瀏覽器里訪問
http://127.0.0.1:8002/hello/
====================================
在 D:\temp-test\djangodemo\ 下創建簡單的項目:
在命令行窗口操作:
cd D:\temp-test\djangodemo\
創建項目 mysite456
django-admin startproject mysite456
啟動項目
python manage.py runserver 8003
瀏覽器里訪問
http://127.0.0.1:8003/
新建 templates 文件夾
site456/
|-- site456
| |-- __init__.py
| |-- settings.py
| |-- urls.py
| |-- views.py
| |-- wsgi.py
|-- manage.py
`-- templates
`-- mytemp.html
新建 mytemp.html 文件
mytemp.html 文件代碼:
<h1>{{ hello }}</h1>
修改 site456/site456/settings.py 文件
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], # 修改位置
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
新建 site456/site456/views.py 文件
views.py 文件代碼:
from django.shortcuts import render
def mytemp(request):
context = {}
context['hello'] = 'Hello World!'
return render(request, 'mytemp.html', context)
site456/site456/urls.py 文件代碼:
from django.urls import path
from . import views
urlpatterns = [
path('mytemp/', views.mytemp),
]
瀏覽器訪問:
http://127.0.0.1:8003/mytemp/
REF
https://www.tutorialspoint.com/django/index.htm
https://www.geeksforgeeks.org/django-tutorial/
https://www.javatpoint.com/django-tutorial