python小白-day15 Django基礎

Python的WEB框架有Django、Tornado、Flask 等多種,Django相較與其餘WEB框架其優點爲:大而全,框架自己集成了ORM、模型綁定、模板引擎、緩存、Session等諸多功能。
css

django簡介

Django是一個開放源代碼的Web應用框架,由Python寫成。採用了MVC的軟件設計模式,即模型M,視圖V和控制器C。它最初是被開發來用於管理勞倫斯出版集團旗下的一些以新聞內容爲主的網站的,便是CMS(內容管理系統)軟件。並於2005年7月在BSD許可證下發布。這套框架是以比利時的吉普賽爵士吉他手Django Reinhardt來命名的。html

Django是一個基於MVC構造的框架。可是在Django中,控制器接受用戶輸入的部分由框架自行處理,因此 Django 裏更關注的是模型(Model)、模板(Template)和視圖(Views),稱爲 MTV模式。它們各自的職責以下:python

模型(Model),即數據存取層linux

處理與數據相關的全部事務: 如何存取、如何驗證有效性、包含哪些行爲以及數據之間的關係等。正則表達式

模板(Template),即表現層sql

處理與表現相關的決定: 如何在頁面或其餘類型文檔中進行顯示。shell

視圖(View),即業務邏輯層數據庫

存取模型及調取恰當模板的相關邏輯。模型與模板之間的橋樑。django

基本結構:
json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
│  db.sqlite3 ----------sqlie3數據庫
│  manage.py       
│     
├─logres
│  │  admin.py          後臺,能夠用不多量的代碼就擁有一個強大的後臺。
│  │  apps.py
│  │  models.py         與數據庫操做相關,存入或讀取數據時用到這個
│  │  tests.py
│  │  urls.py
│  │  views.py 
│  │  處理用戶發出的請求,從urls.py中對應過來, 經過渲染templates中的網頁能夠將顯示
│  │      內容好比登錄後的用戶名,用戶請求的數據,輸出到網頁。
│  │  __init__.py
│  │ 
│  ├─migrations
│     │  0001_initial.py
│     │  __init__.py
│   
│ 
│ 
│ 
├─Mushishi
│  │  settings.py  Django 的設置,配置文件,好比 DEBUG 的開關,靜態文件的位置等
│  │  urls.py    urls.py
│  │             網址入口,關聯到對應的views.py中的一個函數(或者generic類),
│  │             訪問網址就對應一個函數。
│  │  wsgi.py    wsgi有多重一種uwsgi和wsgi,你用那種wsgi來運行Django,
                  通常不用改只有你用到的時候在改
│  │  __init__.py
│  
│         
├─static
└─templates         templates中的Html模板,
         index.html
         login.html
         regist.html

安裝django

1
2
3
4
5
6
7
8
9
10
1 .window 使用pycharm安裝
     過程略
2 .window上使用pip安裝
     pip install django
3 .linux下使用pip安裝
     yum install python-pip
     pip install django= 1.9 . 5
4 .檢查是否安裝成功
>>> import django
>>> django.VERSION

基本命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1 .建立django命令
django-admin.py startproject project-name(你工程的名字)
2 .建立django的app
python manage.py startapp app-name(你app的名字)
或 django-admin.py startapp app-name(你app的名字)
3 .同步數據庫
python manage.py syncdb
注意:Django 1.7 . 1 及以上的版本須要用如下命令
python manage.py makemigrations
python manage.py migrate
4 .調試模式
python manage.py runserver 8001
#監聽全部可用 ip (電腦可能有一個或多個內網ip,一個或多個外網ip,即有多個ip地址)
python manage.py runserver 0.0 . 0.0 : 8000
5 .清除數據庫
python manage.py flush
6 .建立超級管理員
python manage.py createsuperuser
按照提示就ok
7 .修改管理員密碼
python manage.py changepassword username(你當時設定的用戶名)
8 .導入和導出數據
python manage.py dumpdata appname > appname.json
python manage.py loaddata appname.json
9 .進入數據庫
python manage.py dbshell
10 .更多命令
python manage.py

建立django程序

1.建立一個project工程和app

步驟以下


Project和App概念

Project是一個大的工程,

下面有不少功能:(一個Project有多個App,其實他就是對你大的工程的一個分類)

例如一個運維平臺是一個工程,那麼他的app就是CMDB,監控系統,OA系統,

2.生成數據庫 建立超級管理員用戶

注:此步驟是在pycharm的終端裏使用命令實現的

1
2
3
4
5
6
7
8
1 .同步數據庫
python manage.py makemigrations
python manage.py migrate
本人使用的是django1. 9.5 版本
2 .建立超級管理員
python manage.py createsuperuser
3 .運行django
python manage.py runserver 8000

登陸頁面:

1
2
瀏覽器訪問:
http: //127.0.0.1:8000/admin/

3.路由

1.首先在helloword文件夾下(不是app目錄,千萬別寫錯)的urls.py填寫路由規則

1
2
3
4
5
6
7
8
9
from django.conf.urls import url
from django.contrib import admin
#導入app下的view函數
from helloapp import  views
urlpatterns = [
url(r '^admin/' , admin.site.urls),
#當用戶訪問http://127.0.0.1:端口號的時候之間交給helloapp下面的views裏的index函數來處理
url(r '^$' , views.index),
]

4.views函數

1.在helloapp(app)下面的views裏寫一個index函數

1
2
3
4
5
6
7
#Django 在返回的時候須要一層封裝,須要導入HttpResponse
from django.shortcuts import render,HttpResponse
 
# Create your views here.
def index(request):
      #使用HttpRespons 封裝返回信息
     return HttpResponse( '<h1>hello world!!!</h1>' )

django中的路由系統和其餘語言的框架有所不一樣,在django中每個請求的url都要有一條路由映射,這樣才能將請求交給對一個的view中的函數去處理。其餘大部分的Web框架則是對一類的url請求作一條路由映射,從而是路由系統變得簡潔。

2.若是要返回html頁面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1. 在templates裏建立index.html頁面
內容:
 
<!DOCTYPE html>
<html lang = "en" >
<head>
<meta charset = "UTF-8" >
<title>Title< / title>
< / head>
<body>
<h1>Hello world!!< / h1>
< / body>
< / html>
2. 修改helloapp裏的views.py的index函數
 
from django.shortcuts import render,HttpResponse
# Create your views here.
def index(request):
# return HttpResponse('<h1>hello world!!!</h1>')
return  render(request, 'index.html' )
#找到index.html
#讀取index.html返回給用戶

當以上步驟都完成以後,在IDE終端中輸入:

1
python manage.py runserver 8000

訪問:http://127.0.0.1:8000

django路由系統

在Django的urls中咱們能夠根據一個URL對應一個函數名來定義路由規則以下:

每個urls對應一個views裏的函數

1.基本的urls對應

1
2
3
4
5
urlpatterns = [
url(r '^login/$' , views.login),
url(r '^index/$' , views.index),
url(r '^$' , views.login),
]

2.基於app的路由

根據app對路由規則進行一次分類

當app的urls不少的時候,那麼就不能再工程的urls下面去設置
應該這樣設置:

1.首先在helloword下面的urls這樣設置

1
2
3
4
5
6
7
8
9
10
11
#導入include
from django.conf.urls import url,include
from django.contrib import admin
#導入app下的view函數
from helloapp import  views
urlpatterns = [
     url(r '^admin/' , admin.site.urls),
     #使用helloapp的urls規則
     url(r '^helloapp/' ,include( 'helloapp/urls' ))
 
]

2.在helloapp下面建立一個urls.py

內容:

1
2
3
4
5
6
7
8
9
10
11
from django.conf.urls import url,include
from django.contrib import admin
#導入app下的view函數
from . import views
urlpatterns = [
     url(r '^admin/' , admin.site.urls),
     url(r '^login/' ,views.login),
     url(r '^index/' ,views.index),
     url(r '^reg/' ,views.reg),
     url(r '^layout/' ,views.layout),
]

3.在helloapp下面的views裏建立上面urls對應的函數

1
2
3
4
5
6
7
8
9
10
11
from django.shortcuts import render,HttpResponse
# Create your views here.
def index(request):
     # return HttpResponse('<h1>hello world!!!</h1>')
     return  render(request, 'index.html' )
def login(request):
     return HttpResponse( 'login' )
def reg(request):
     return HttpResponse( 'reg' )
def layout(request):
     return HttpResponse( 'layout' )

4.訪問:

1
2
3
4
5
http: //127.0.0.1:8000/helloapp/admin/ admin後臺管理
http: //127.0.0.1:8000/helloapp/layout/
http: //127.0.0.1:8000/helloapp/login/
http: //127.0.0.1:8000/helloapp/reg/
http: //127.0.0.1:8000/helloapp/index/

3.動態路由(傳一個參數)

好比分頁:當urls大量過多的時候好比幾百個的時候,那麼確定不會去寫幾百個路由規則
全部這個時候就須要動態urls,使用正則表達式來完成

1.在helloapp下面的urls寫入如下內容:

1
2
3
4
5
6
7
8
from django.conf.urls import url,include
from django.contrib import admin
#導入app下的view函數
from . import views
urlpatterns = [
     url(r '^admin/' , admin.site.urls),
     url(r '^book/(\d+)$' , views.book), / / 正則匹配
]

2.在helloapp下面的views寫入如下內容:

1
2
3
4
5
6
7
from django.shortcuts import render,HttpResponse
# Create your views here.
def book(request,num):
     print (num)
     return HttpResponse(num)
當用戶訪問http: / / 127.0 . 0.1 : 8000 / helloapp / book / 數字的時候
django會在自動把參數傳給views裏的book函數

3.測試訪問:

1
2
3
4
5
6
7
http: / / 127.0 . 0.1 : 8000 / helloapp / book / 2
 
顯示 2
 
http: / / 127.0 . 0.1 : 8000 / helloapp / book / 1
 
顯示 1

4.動態路由(傳多個參數)

多個參數它是已/來分割的

來一個url的加法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1. 在helloapp下面的urls寫入如下內容:
 
from django.conf.urls import url,include
from django.contrib import admin
#導入app下的view函數
from . import views
urlpatterns = [
     url(r '^admin/' , admin.site.urls),
     url(r '^book/(\d+)/(\d+)$' , views.book),
]
2. 在helloapp下面的views寫入如下內容:
 
def book(request,num1,num2):
     print (num1,num2)
     num = int (num1) + int (num2)
     return HttpResponse(num)
3. 測試訪問:
http: / / 127.0 . 0.1 : 8000 / helloapp / book / 2 / 1
顯示 3
http: / / 127.0 . 0.1 : 8000 / helloapp / book / 2 / 10
顯示 12

他的順序是:正序的,你先給他傳那個值,第一個參數就是那個

5.動態的路由(Key:value的形式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
1. 在helloapp下面的urls寫入如下內容:
 
from django.conf.urls import url,include
from django.contrib import admin
#導入app下的view函數
from . import views
urlpatterns = [
     url(r '^admin/' , admin.site.urls),
     url(r '^book/(?P<k1>\d+)/(?P<k2>\d+)$' , views.book),
     這裏?p<v1>這裏的v1就是key,vlaue就是傳進去的值,
]
2. 在helloapp下面的views寫入如下內容:
 
from django.shortcuts import render,HttpResponse
# Create your views here.
def book(request,k1,k2):
     print (k1,k2)
     return HttpResponse(k1 + k2)

這樣咱們就沒必要按照順序去取了,能夠經過key,value的方式來取傳進來的值

注:能夠根據傳來的值來進行判斷返回給用戶指定的url,過程略。

6.基於反射的動態路由

僅僅是經過反射來實現的,經過文件找到裏面的函數而後執行!
可是在Django中不建議使用此方法。由於不一樣的WEB框架建議你使用不一樣的方式,
Django就不建議使用反射

模板引擎

django使用jinja2爲模板引擎

使用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{{ item }}
{% for item in item_list %}  < a >{{ item }}</ a >  {% endfor %}
forloop.counter
forloop.first
forloop.last
{% if ordered_warranty %}  {% else %} {% endif %}
母板:{% block title %}{% endblock %}
子板:{% extends "base.html" %}
{% block title %}{% endblock %}
幫助方法:
{{ item.event_start|date:"Y-m-d H:i:s"}}
{{ bio|truncatewords:"30" }}
{{ my_list|first|upper }}
{{ name|lower }}

1.{item}

在helloapp下面的urls寫入如下內容:

1
2
3
4
5
6
7
from django.conf.urls import url,include
from django.contrib import admin
from . import  views
urlpatterns = [
     url(r '^admin/' , admin.site.urls),
     url(r '^index/' ,views.index),
]

在helloapp下面views裏面寫入如下內容:

1
2
3
4
5
6
from django.shortcuts import render,HttpResponse
from django.shortcuts import redirect
# Create your views here.
def index(request):
     dic = { 'name' : 'hetan' , 'age' : '26' }
     return render(request, 'index.html' ,dic)

以後再index.html下面寫入如下內容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title >Title</ title >
</ head >
< body >
< div style = "background-color:red ;height: 100px" >
     < h1 >{{ name }}</ h1 >
     < h1 >{{ age }}</ h1 >
     < h1 >{{ job }}</ h1 >
</ div >
</ body >
</ html >

2.{% for item in item_list %} {{ item }} {% endfor %}

修改helloapp下面的views

1
2
3
4
5
6
from django.shortcuts import render,HttpResponse
from django.shortcuts import redirect
# Create your views here.
def index(request):
     dic = { 'userlist' :[ 'mayun' , 'alex' , 'wusir' , 'hetan' ]}
     return render(request, 'index.html' ,dic)

修改index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
< html lang = "en" >
< head >
     < meta charset = "UTF-8" >
     < title >Title</ title >
</ head >
< body >
< div style = "background-color:gray ;height:300px" >
     {% for item in userlist %}
     < h1 >{{ item }}</ h1 >
     {% endfor %}
</ div >
</ body >
</ html >

3.{% if ordered_warranty %} {% else %} {% endif %}

修改helloapp下面的views

1
2
3
4
5
6
from django.shortcuts import render,HttpResponse
from django.shortcuts import redirect
# Create your views here.
def index(request):
     dic = { 'user' : 'hetan' , 'age' : 26 }
     return render(request, 'index.html' ,dic)

修改index.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
< html lang = "en" >
< head >
     < meta charset = "UTF-8" >
     < title >Title</ title >
</ head >
< body >
< div style = "background-color:gray ;height:300px" >
     {% if user == 'hetan' %}
     < h1 >{{ user }}</ h1 >
     {% else %}
     < h1 >你輸入的有問題</ h1 >
     {% endif %}
</ div >
</ body >
</ html >

4.母板和子板:

母版:{% block title %}{% endblock %}

子板:{% extends "base.html" %}


上面、左側的紅色框體都沒有變,變得是中間的內容是怎麼實現的呢?就是經過母版來實現

1.建立母板

在django裏通常在templates裏建立一個名字叫master的文件夾裏存放母版的html頁面
並且母版只有一個,不知道能不能多個母版,可是爲了統一和使用因此只使用一個母版


master/mamaindex.html頁面內容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title >Title</ title >
< style >
     .header{
         height:100px;
         border: 1px solid rebeccapurple;
         background-color: #d3ffa4;
     }
     .header h1{
         display: block;
         margin-left:10px;
         font-size:50px;
     }
     .menu{
         background-color:dodgerblue;
         height:500px;;
         float: left;
         width: 20%;
         border: 1px solid black;
     }
     .menu ul{
         margin:0;
         padding: 0;
     }
     .menu ul li{
         list-style-type: none;
         width:100%;
         height:50px;
         background-color: aquamarine;
         border-bottom: 1px solid rebeccapurple;
 
     }
     .menu ul li a{
         font-size:20px;
         line-height:50px;
     }
     .content{
         background-color:slateblue;
         float: left;
         width:70%;
     }
</ style >
</ head >
< body >
< div class = "header" >< h1 >Hetan-django</ h1 ></ div >
     < div class = "menu" >
         < ul >
             < li >< a >菜單1</ a ></ li >
             < li >< a >菜單2</ a ></ li >
             < li >< a >菜單3</ a ></ li >
             < li >< a >菜單4</ a ></ li >
             < li >< a >菜單5</ a ></ li >
         </ ul >
     </ div >
     < div class = "content" >
         {#可變的子版內容,這個content和class content無關#}
         {% block content %} {% endblock %}
     </ div >
</ body >
</ html >

2.修改index.html(子板)

1
2
3
4
5
{% extends 'master/mamaindex.html' %}
 
{% block content %}
< h1 >這些一個變化的子板內容</ h1 >
{% endblock %}

5.導入公共標籤
若是有公共的模板須要在不一樣的html裏使用。那麼可使用導入公共的方式,避免重複代碼

步驟:

在templates裏
建立一個include目錄(名字能夠隨意定義),放入公共的html頁面

1.在include裏新建一個inindex.html頁面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
< html lang = "en" >
< head >
     < meta charset = "UTF-8" >
     < title >Title</ title >
</ head >
< body >
< h1 >我是一個公共的內容</ h1 >
< form >
     < input type = "text" >
     < input type = "password" />
     < input  type = "button" />
</ form >
</ body >
</ html >

2.在index.html使用

1
2
3
4
5
6
{% extends 'master/mamaindex.html' %}
 
{% block content %}
< h1 >這些一個變化的子板內容</ h1 >
{% include 'include/inindex.html' %}
{% endblock %}

Django靜態文件配置

把全部的靜態都放在static目錄下,好比:css、js、imgs、等


配置引入static目錄,
在settings裏,不然沒法使用static目錄下的靜態文件,
由於他找不到路徑!的須要告訴django

在公共配置文件settings中最後添加:

1
2
3
4
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static' ),
)

1.母版使用css,子板能夠繼承母版的css

2.首先在工程目錄下建立static

3.在static裏建立css目錄用來存放css文件

4.在settings裏配置static目錄

5.在static目錄下建立mama.css
放入一下內容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
.header{
         height:100px;
         border: 1px solid rebeccapurple;
         background-color: #d3ffa4;
     }
     .header h1{
         display: block;
         margin-left:10px;
         font-size:50px;
     }
     .menu{
         background-color:dodgerblue;
         height:500px;;
         float: left;
         width: 20%;
         border: 1px solid black;
     }
     .menu ul{
         margin:0;
         padding: 0;
     }
     .menu ul li{
         list-style-type: none;
         width:100%;
         height:50px;
         background-color: aquamarine;
         border-bottom: 1px solid rebeccapurple;
 
     }
     .menu ul li a{
         font-size:20px;
         line-height:50px;
     }
     .content{
         background-color:slateblue;
         float: left;
         width:70%;
     }

6.在templates裏的master裏的mamaindex.html(母版)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
#這個可讓子板本身使用本身的title
< title >{% block title %}母版{% endblock %}</ title >
#引用css文件
< link rel = "stylesheet" href = "/static/css/mama.css" />
#而後在模板裏,咱們也會寫一個block,若是子版裏有須要使用本身的css樣式能夠本身定義#}
#{% block css %} 內容 {% endblock %}
</ head >
< body >
< div class = "header" >< h1 >Yao-django</ h1 ></ div >
     < div class = "menu" >
         < ul >
             < li >< a >菜單1</ a ></ li >
             < li >< a >菜單2</ a ></ li >
             < li >< a >菜單3</ a ></ li >
             < li >< a >菜單4</ a ></ li >
             < li >< a >菜單5</ a ></ li >
         </ ul >
     </ div >
     < div class = "content" >
         {#可變的子版內容,這個content和class content無關#}
         {% block content %} {% endblock %}
     </ div >
</ body >
% block js %} 可使用本身的js {% endblock %}
</ html >

7.templates裏的index.html寫入如下內容:

1
2
3
4
5
6
{% extends 'master/mamaindex.html' %}
{% block title %}子板{% endblock %}
{% block content %}
< h1 >這些一個變化的子板內容</ h1 >
{% include 'include/inindex.html' %}   
{% endblock %}

django的Model基本操做和增、刪、改、查

注:默認使用了sqlite3數據庫

若是想使用其餘數據庫,請在settings裏修改

1.建立數據庫:

一、建立model類

在helloapp(或者你的app下)下models.py寫入如下內容:

1
2
3
4
5
6
7
8
9
from django.db import models
# Create your models here.
#這個類是用來生成數據庫表的,這個類必須集成models.Model
class UserInfo(models.Model):
     #建立表的字段
     username = models.CharField(max_length = 16 )
     #這個就表示去數據庫建立一個字符串類型的字段
     password = models.CharField(max_length = 32
     #對於字符串類型的字段必須設置一個最大長度

2.註冊app(若是沒註冊,那麼不能建立數據庫表)

1
請在settings裏的INSTALLED_APPS裏面加上你的app名字

3.建立數據庫結構

在IDE終端輸入:

1
2
python  manage.py makemigrations 生成一個數據庫結構migrations裏面一個表
python  manage.py migrate       根據migrations裏面的表來建立數據庫

4.爲了方便查詢,因此將數據庫註冊到後臺。經過admin後臺來管理

1
2
3
4
5
6
7
8
9
10
在helloapp下面的admin.py寫入如下內容:
 
from django.contrib import admin
 
# Register your models here.
#導入helloapp的數據庫模塊
from . import models
 
#註冊我們建立的類,經過他來訪問
admin.site.register(models.UserInfo)

2.增長數據

使用頁面來進行註冊用戶和密碼插入數據庫

1.插入一個urls在helloapp下面的urls.py

1
url(r '^reg/' ,views.reg),

2.在templates裏面建立一個reg.html頁面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title >Title</ title >
</ head >
< body >
< form action = "/helloapp/reg/" method = "post" >
     < h1 >註冊頁面</ h1 >
     用戶名:< input type = "text" />
     密 碼:< input type = "password" />
     < input type = "submit" value = "註冊" />
</ form >
</ body >
</ html >

3.在settings裏註釋中間件


4.在helloapp下的views裏寫入如下內容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from django.shortcuts import render,HttpResponse
from django.shortcuts import redirect
#導入modles
from . import models
# Create your views here.
def index(request):
     dic = { 'user' : 'liuyao' , 'age' : 10 }
     return render(request, 'index.html' ,dic)
def reg(request):
     if request.method = = 'POST' :
         reg_user = request.POST[ 'username' ]
         reg_pwd = request.POST[ 'password' ]
         # 增長
         models.UserInfo.objects.create(username = reg_user, password = reg_pwd)
         # dic = {"username": 'hetan', "password": '123',}
         #models.UserInfo.objects.create(**dic)
         return  redirect( '/helloapp/index' )
     else :
         return render(request, 'reg.html' )
在後臺管理能夠看到此用戶名和密碼

3.刪除

1
2
#根據條件刪除
# models.UserInfo.objects.filter(username='yaoyao').delete()

4.修改

1
2
# 修改
# models.UserInfo.objects.all().update(age=18)

4.查詢

1
2
3
4
5
6
7
8
9
10
#查找全部
# models.UserInfo.objects.all()
#查找指定條件
# models.UserInfo.objects.filter(age=18)
#查找第一個
# models.UserInfo.objects.filter(age=18).first()
#查找全部而且顯示出來
# user_list_obj = models.UserInfo.objects.all()
# for line in user_list_obj:
#     print(line.username,line.age)





相關文章
相關標籤/搜索