绗竴姝ワ細瀹夎Django
Django鏄竴涓珮绾ython Web妗嗘灦锛屽畠浣垮緱Web寮鍙戞洿鍔犲揩閫熷拰绠鍗曘傛偍鍙互浣跨敤pip锛圥ython鐨勫寘绠$悊鍣級鏉ュ畨瑁匘jango銆傛墦寮缁堢鎴栧懡浠ゆ彁绀虹锛屽苟杈撳叆浠ヤ笅鍛戒护锛
pip install django
绗簩姝ワ細鍒涘缓Django椤圭洰
瀹夎瀹孌jango鍚庯紝鎮ㄥ彲浠ヤ娇鐢ㄤ互涓嬪懡浠ゅ垱寤轰竴涓柊鐨凞jango椤圭洰锛
django-admin startproject myproject
杩欏皢鍦ㄥ綋鍓嶇洰褰曚笅鍒涘缓涓涓悕涓衡渕yproject鈥濈殑鏂囦欢澶癸紝鍏朵腑鍖呭惈鍩烘湰鐨凞jango椤圭洰缁撴瀯銆
绗笁姝ワ細閰嶇疆鏁版嵁搴
鍦―jango涓紝鏁版嵁搴撻厤缃氬父鍦ㄢ渕yproject/settings.py鈥濇枃浠朵腑杩涜銆傞粯璁ゆ儏鍐典笅锛孌jango浣跨敤SQLite鏁版嵁搴撱傛牴鎹偍鐨勯渶姹傦紝鎮ㄥ彲浠ラ夋嫨鍏朵粬鏁版嵁搴撶郴缁燂紙濡侻ySQL銆丳ostgreSQL绛夛級銆傚湪鈥淒ATABASES鈥濊缃腑鎸囧畾鏁版嵁搴撻厤缃
绗洓姝ワ細鍒涘缓搴旂敤
鍦ㄢ渕yproject鈥濇枃浠跺す涓紝鎵撳紑缁堢鎴栧懡浠ゆ彁绀虹锛屽苟杈撳叆浠ヤ笅鍛戒护鏉ュ垱寤轰竴涓柊鐨勫簲鐢細
python manage.py startapp myapp
杩欏皢鍦ㄢ渕yproject/myapp鈥濇枃浠跺す涓垱寤轰竴涓柊鐨勫簲鐢ㄧ粨鏋勩
绗簲姝ワ細閰嶇疆URL璺敱
鍦―jango涓紝URL璺敱瀹氫箟浜嗙綉绔欑殑缁撴瀯鍜屽姛鑳姐傛墦寮鈥渕yproject/urls.py鈥濇枃浠讹紝骞舵坊鍔犱互涓嬩唬鐮佷互瀹氫箟URL璺敱锛
from django.urls import path
from . import views
urlpatterns = [path(鈥樷, views.home, name=鈥檋ome鈥), path(鈥榓bout/鈥, views.about, name=鈥檃bout鈥)]
杩欓噷瀹氫箟浜嗕袱涓猆RL璺敱锛氭牴璺緞锛堚/鈥橈級鍜屸/about/鈥濊矾寰勩傚畠浠垎鍒槧灏勫埌鈥渧iews.home鈥濆拰鈥渧iews.about鈥濊鍥惧嚱鏁般
绗叚姝ワ細缂栧啓瑙嗗浘鍑芥暟
鍦ㄢ渕yapp/views.py鈥濇枃浠朵腑锛屾偍鍙互缂栧啓瑙嗗浘鍑芥暟鏉ュ鐞嗚姹傚拰鐢熸垚鍝嶅簲銆備互涓嬫槸涓涓畝鍗曠殑绀轰緥锛
from django.shortcuts import render
from django.http import HttpResponse
def home(request):
return render(request, 鈥榤yapp/home.html鈥)
def about(request):
return render(request, 鈥榤yapp/about.html鈥)
杩欓噷瀹氫箟浜嗕袱涓鍥惧嚱鏁帮細home鍜宎bout銆傚畠浠兘浣跨敤render鍑芥暟鏉ュ憟鐜扮浉搴旂殑HTML妯℃澘锛堚檓yapp/home.html鈥欏拰鈥檓yapp/about.html鈥欙級銆
绗竷姝ワ細鍒涘缓HTML妯℃澘锛堝彲閫夛級
濡傛灉鎮ㄧ殑瑙嗗浘鍑芥暟闇瑕佸憟鐜板姩鎬佸唴瀹癸紝鎮ㄩ渶瑕佸垱寤篐TML妯℃澘鏂囦欢銆傚湪鈥渕yapp鈥濇枃浠跺す涓垱寤轰竴涓悕涓衡渢emplates鈥濈殑鏂囦欢澶癸紝骞跺湪鍏朵腑鍒涘缓鈥渉ome.html鈥濆拰鈥渁bout.html鈥濇枃浠躲備互涓嬫槸涓涓畝鍗曠殑HTML妯℃澘绀轰緥锛
Welcome to the Home Page!
About Us
This is the About Page.
绗叓姝ワ細杩愯Web鏈嶅姟鍣
鏈鍚庝竴姝ユ槸杩愯Web鏈嶅姟鍣紝浠ヤ究鎮ㄥ彲浠ュ湪Web娴忚鍣ㄤ腑璁块棶鎮ㄧ殑缃戠珯銆傚湪鈥渕yproject鈥濇枃浠跺す涓墦寮缁堢鎴栧懡浠ゆ彁绀虹锛屽苟杈撳叆浠ヤ笅鍛戒护锛
serve django server development
浠ヤ笂灏辨槸Python Web鏈嶅姟鍣ㄥ叆闂細浠庢惌寤哄埌杩愯Django妗嗘灦鐨勮缁嗗唴瀹癸紝鏇村淇℃伅璇峰叧娉∣D浜戝叾瀹冪浉鍏虫枃绔狅紒