天天看點

Django--model模型綁定_資料庫操作

1、建立model類

app01/models.py

1 2 3 4 5 6 7

from

django.db

import

models

# Create your models here.

class

UserInfo(models.Model): 

#建立model類,必須繼承自models.Model類

# 設計表結構和字段,最大長度,預設表名app01_userinfo

email

=

models.CharField(max_length

=

16

)

pwd

=

models.CharField(max_length

=

20

)

2、注冊app

settings.py

1 2 3 4 5 6 7 8 9

INSTALLED_APPS = [

'django.contrib.admin'

,

'django.contrib.auth'

,

'django.contrib.contenttypes'

,

'django.contrib.sessions'

,

'django.contrib.messages'

,

'django.contrib.staticfiles'

,

'app01'

,

]

3、執行指令建立表

cmd> python manage.py makemigrations       生成源資訊,一個字典,位置:app01/migrations/0001_initial.py

cmd> python manage.py migrate     讀取生成的源資訊,去生成資料庫表

4、檢視

4.1、用navicat-SQLite工具檢視
Django--model模型綁定_資料庫操作
4.2、用程式檢視

app01/admin.py

1 2

from

app01

import

models

admin.site.register(models.UserInfo)     #把UserInfo建立的表注冊進admin頁面

浏覽器通路http://127.0.0.1:8000/admin/

Django--model模型綁定_資料庫操作

點進去後就可以添加使用者

Django--model模型綁定_資料庫操作

5、現在有一個使用者user1,登入成功後跳轉到index頁面,列出資料庫表裡的所有使用者資訊。

app01/views.py

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

from

django.shortcuts

import

render

from

django.shortcuts

import

redirect

def

login(request):

if

request.method

=

=

'POST'

:

input_email

=

request.POST[

'email'

]

input_pwd

=

request.POST[

'pwd'

]

if

input_email

=

=

'[email protected]'

and

input_pwd

=

=

'123'

:

return

redirect(

"/index/"

)      #登入成功後跳轉到index頁面

else

:

return

render(request,

"login.html"

,{

"status"

:

"使用者名密碼錯誤"

})

return

render(request,

'login.html'

)

def

index(request):

#從資料庫中擷取資料,并和html渲染

from

app01

import

models

#擷取表中所有資料

user_info_list

=

models.UserInfo.objects.

all

()

return

render(request,

'index.html'

,{

"user_info_list"

:user_info_list,})

urls.py

1 2 3 4 5 6 7 8

from

django.conf.urls

import

url

from

django.contrib

import

admin

from

app01

import

views

urlpatterns

=

[

url(r

'^admin/'

, admin.site.urls),

url(r

'^login/'

, views.login),

url(r

'^index/'

, views.index),

]

templates/index.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

<!DOCTYPE html>

<

html

lang

=

"en"

>

<

head

>

<

meta

charset

=

"UTF-8"

>

<

title

></

title

>

</

head

>

<

body

>

<

div

>

<

table

>

<

thead

>

<

tr

>

<

th

>郵箱</

th

>

<

th

>密碼</

th

>

</

tr

>

</

thead

>

<

tbody

>

{% for line in user_info_list %}

<

tr

>

<

td

>{{ line.email }}</

td

>

<

td

>{{ line.pwd }}</

td

>

</

tr

>

{% endfor %}

</

tbody

>

</

table

>

</

div

>

</

body

>

</

html

>

浏覽器通路 http://127.0.0.1:8000/login/

Django--model模型綁定_資料庫操作

登入成功後跳轉到 http://127.0.0.1:8000/index/,列出所有的剛才手動在頁面建立的使用者。

Django--model模型綁定_資料庫操作

6、model添加資料

index.html

1 2 3 4 5

<

form

action

=

"/index/"

method

=

"post"

>

<

input

type

=

"text"

name

=

"em"

/>

<

input

type

=

"text"

name

=

"pw"

/>

<

input

type

=

"submit"

value

=

"添加"

/>

</

form

>

views.py

1 2 3 4 5 6 7 8 9 10 11

def index(request):

#從資料庫中擷取資料,并和html渲染

from app01

import

models

#添加資料

if

request.method==

'POST'

:

input_em = request.POST[

'em'

]

input_pw = request.POST[

'pw'

]

models.UserInfo.objects.create(email=input_em,pwd=input_pw)

#擷取表中所有資料

user_info_list = models.UserInfo.objects.all()

return

render(request,

'index.html'

,{

"user_info_list"

:user_info_list,})

7、model删除資料

index.html

1 2 3 4 5

<

form

action

=

"/index/"

method

=

"post"

>

<

input

type

=

"text"

name

=

"email_del"

>

<

input

type

=

"text"

name

=

"pwd_del"

>

<

input

type

=

"submit"

value

=

"删除"

>

</

form

>

views.py​

1 2 3 4 5 6

def

index(request):

from

app01

import

models

#删除資料

if

request.method

=

=

'POST'

:

input_email

=

request.POST[

'email_del'

]

models.UserInfo.objects.

filter

(email

=

input_email).delete()

8、model更新資料

index.html

1 2 3 4 5

<form action=

"/index/"

method=

"post"

>

<input type=

"text"

name=

"email_update"

>

<input type=

"text"

name=

"pwd_update"

>

<input type=

"submit"

value=

"更新"

>

</form>

views.py

1 2 3 4 5 6

def

index(request):

#更新資料

if

request.method

=

=

'POST'

:

input_em

=

request.POST[

'email_update'

]

input_pw

=

request.POST[

'pwd_update'

]

models.UserInfo.objects.

filter

(email

=

input_em).update(pwd

=

input_pw)

9、頁面驗證:

Django--model模型綁定_資料庫操作

來自為知筆記(Wiz)

轉載于:https://www.cnblogs.com/daliangtou/p/5262872.html