目前的django知識都是源自對Resdig這個項目的開發(fā)過程的摸索
先ad一波
找各種電影視頻資源歡迎到Resdig.net資源挖掘機
進入正題 DJANGO開發(fā)環(huán)境搭建流程
00_django安裝
命令行輸入
pip install django
如果你是linux 并使用python3 可能需要將pip換成pip3
01_項目
創(chuàng)建一個項目
django-admin.py startproject testproject
認識項目目錄
此時你會看到當前目錄出現(xiàn)了一個文件夾
這個文件夾就是我們的剛創(chuàng)建的項目文件夾
其內(nèi)部結(jié)構(gòu)如下
testproject/manage.py 是一個項目管理腳本 后面會經(jīng)常用到
testproject/testproject/ 內(nèi)部結(jié)構(gòu)如下
在本教程中我們只需要關(guān)注該目錄下的兩個文件
testproject/testproject/setting.py 這個文件是整個項目的配置文件
testproject/testproject/urls.py 這個文件是整個項目的根路由配置文件
運行服務(wù)并測試
進入 testproject/ 并在該目錄打開powershell或cmd
用python調(diào)用manage.py 讓在本地主機80端口啟動服務(wù)
python manage.py runserver 0.0.0.0:80
你會看到
2020-2-27 update:
這里的紅字提示有數(shù)據(jù)庫沒有遷移(django默認使用sqlite3數(shù)據(jù)庫特石,一開始數(shù)據(jù)庫并沒有建立),運行以下命令同步數(shù)據(jù)庫。
python manage.py makemigrations
運行以下命令遷移數(shù)據(jù)庫
python manage.py migrate
然后那行紅字會消失夜郁。接著執(zhí)行上面 runserver操作即可。
關(guān)于數(shù)據(jù)庫贞岭,后面再說猾普。
訪問 127.0.0.1(linux 0.0.0.0)
這就ok了
應(yīng)用的創(chuàng)建,認識,基本修改與測試
創(chuàng)建
什么是應(yīng)用? 前輩之前舉過一個例子:如果說項目project是一個大學(xué),那么應(yīng)用就是學(xué)院,每個學(xué)院有自己的特定工作.
進入目錄 testproject/ 打開命令行
python manage.py startapp testapp
此時你會看到出現(xiàn)了一個 testapp文件夾 這就是我們的應(yīng)用文件夾
認識
進入testproject/testapp/你會看到這樣的目錄結(jié)構(gòu)
考慮到入門在這里我們只關(guān)注一個文件
testproject/testapp/views.py 它叫視圖 其中的函數(shù)是處理請求并作出響應(yīng)的函數(shù) 是我們后端開發(fā)中的主戰(zhàn)場之一
番外篇------------------Django的 MTV 開發(fā)模式
M: models 模型 指的是數(shù)據(jù)庫
T: templates 模板 指的是 html 模板
V: views 視圖 指的是views.py 處理函數(shù)
用一句話來概括 views/V視圖得到一個請求/request后對請求進行一些操作與判斷,然后調(diào)用合適的模板templates/T,并在數(shù)據(jù)庫models/M中提取相應(yīng)的數(shù)據(jù)合成一個響應(yīng)
通俗的來說 顧客喊話(請求/request)來份大盤雞 大廚(視圖/views/V)翻出菜譜(模板/templates/T) 并從倉庫(模型/models/M)中拿出調(diào)料雞肉(數(shù)據(jù))做出一份大盤雞(響應(yīng))
現(xiàn)在 我們來完善一下文件 在testproject/testapp/ 下建立 一個urls.py
此文件是testapp的url文件 由這個文件可以將請求指向views.py下的具體處理函數(shù)
下面我們來一步一步配置一下url 理解下一個請求django是如何處理的
01_修改根路由文件 testproject/testproject/urls.py
"""testproject URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include#引入include
urlpatterns = [
path('admin/', admin.site.urls),
path('testapp/', include('testapp.urls')),#如果請求的路徑是'/testapp'則指向testapp的路由文件
]
02_修改testapp的路由文件 testproject/testapp/urls.py
from django.urls import path #引入path
from . import views #引入testapp的views
urlpatterns = [
path('', views.home),#將testapp/路徑指向函數(shù)home
path('page_1/', views.page_1),#將testapp/page_1路徑指向函數(shù)page_1
]
03_修改views中的處理函數(shù)
from django.shortcuts import render
from django.http import HttpResponse,JsonResponse,HttpResponseNotAllowed,HttpResponseBadRequest#引入響應(yīng)類
# Create your views here.
#定義你的函數(shù) 以請求request為輸入 以某個響應(yīng)類作為返回
def home(request):
return HttpResponse('hello world! homepage is ok!')
def page_1(request):
return HttpResponse('this is a testapp in testproject! page_1 is ok!')
這樣我們可以看出 django是根據(jù)訪問路徑將請求通過根urls.py指向具體應(yīng)用的urls.py 再由應(yīng)用的urls.py指向應(yīng)用的具體處理函數(shù) 處理函數(shù)處理后做出一個響應(yīng)
測試
運行服務(wù)
PS D:\project\testproject> python manage.py runserver 0.0.0.0:80
Performing system checks...
System check identified no issues (0 silenced).
You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
August 13, 2018 - 12:01:24
Django version 2.0.7, using settings 'testproject.settings'
Starting development server at http://0.0.0.0:80/
Quit the server with CTRL-BREAK.
訪問127.0.0.1/testapp/
訪問127.0.0.1/testapp/page_1/
命令行中會顯示訪問記錄
[13/Aug/2018 12:06:24] "GET /testapp/ HTTP/1.1" 200 29
[13/Aug/2018 12:06:37] "GET /testapp/page_1/ HTTP/1.1" 200 48
model 模型/數(shù)據(jù)庫
打開models.py在這里你可以設(shè)置你的數(shù)據(jù)庫
from django.db import models
# Create your models here.
關(guān)于CSRF
......................................未完待續(xù)...............................
關(guān)于allow_host的問題。麻诀。。傲醉。
關(guān)于csrf的問題
我們可以在
Forbidden (CSRF cookie not set.): /api/