1.環境搭建
1.1 vs2019專業版(安裝時選擇python模塊)
1.2 python:3.7.4(這個要弄環境變量)
1.3 Django模塊:3.0.7(python安裝目錄/Lib/site-packages/django/init.py VERSION)
如果不會加模塊,請點擊這裏
2.新建項目測試環境:
2.1 vs新建項目,選擇Django Web項目
2.2 cmd進入項目目錄下執行
python manage.py runserver 0.0.0.0:8000
2.3 打開瀏覽器輸入http://127.0.0.1:8000/
如果出現錯誤:TemplateSyntaxError at /
‘staticfiles’ is not a registered tag library.
往後找到Error during template rendering提示\layout.html, error at line 7
找到對應文件將
{% load staticfiles %}
改爲
{% load static%}
2.4 在Django2(項目名稱)目錄下添加myviews.py文件作爲視圖文件
"""
file: myviews.py
author: hehl
time: 2020.6.4
qq/vx: 845367567
remarks: 視圖
"""
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world ! ")
2.5 找到同級目錄下urls.py文件,將原有內容刪掉,並修改爲以下內容
"""
file: urls.py
author: hehl
time: 2020.6.4
qq/vx: 845367567
remarks: url配置
"""
from django.conf.urls import url
from . import myviews
urlpatterns = [
url(r'^$', myviews.hello), # 缺省正則表達式 http://127.0.0.1:8000
url('hello', myviews.hello), # 修改規則 url/hello訪問 http://127.0.0.1:8000/hello
url("ini", myviews.hello), # http://127.0.0.1:8000/ini
url("hehl/test", myviews.hello),# http://127.0.0.1:8000/hehl/test
]
2.6 再刷新瀏覽器(不需要重啓服務,自動檢測代碼修改)
如下圖所示即爲成功
3.Django模板
3.1 項目目錄下新建文件夾mytemplates,該目錄下新建文件hehl.html、hehl1.html、hehl2.html、hehl3.html
3.2向Django說明模板文件的路徑
修改項目目錄下Django2/settings.py,修改 TEMPLATES 中的 DIRS 爲 [BASE_DIR+"/templates",]