web框架的功能:javascript
1.socket收發消息 (wsgi)css
2.根據不一樣的地址返回不一樣的內容html
3.返回動態頁面(字符串的替換)前端
web框架的分類: java
1. gjango + wsgirefpython
2. flask janja2mysql
3. tornado .web
Django官網下載頁面:https://www.djangoproject.com/download/sql
安裝最新LTS版:數據庫
pip3 install django==1.11.15
建立django項目:
django-admin startproject mysite #建立一個名爲mysite 的項目 或用pycharm建立 (命令建立的沒有頁面的路徑)
目錄介紹
qqq/ ├── manage.py # 管理文件 └── qqq # 項目目錄 ├── __init__.py ├── settings.py # 配置 ├── urls.py # 路由 --> URL和函數的對應關係 └── wsgi.py # runserver命令就使用wsgiref模塊作簡單的web server
啓動項目
python manage.py runserver 127.0.0.1:8000 python manage.py runserver 80 #指定80端口啓動
setting.py 配置文件
""" Django settings for qqq project. Generated by 'django-admin startproject' using Django 1.11.15. For more information on this file, see https://docs.djangoproject.com/en/1.11/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/1.11/ref/settings/ """ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #得到項目根目錄的路徑 # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 't8$e3*a^7@)zgh@1et(om%b3xtmj8!9$+^tma=m_(zy#qb7w0%' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True #調試功能 開發時爲True 上線時改成False ALLOWED_HOSTS = ['*'] #容許訪問的主機 '*' 表示全部 # Application definition INSTALLED_APPS = [ #配置app 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles',
'app1.apps.App01config' #將建立的app加入進來 ] MIDDLEWARE = [ #配置中間件 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', #'django.middleware.csrf.CsrfViewMiddleware', #提供保護post請求的中間件,註釋掉才能使用post請求 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'qqq.urls' #url路由的位置 TEMPLATES = [ #模板 { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')] #模板的位置) , 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'qqq.wsgi.application' # Database # https://docs.djangoproject.com/en/1.11/ref/settings/#databases DATABASES = { #數據庫的配置 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } # Password validation # https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, ] # Internationalization # https://docs.djangoproject.com/en/1.11/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.11/howto/static-files/ STATIC_URL = '/static/' #靜態文件的別名,找靜態文件會在這個目錄下的各個子目錄中找
STATICFILES_DIRS = [ #靜態文件的位置
os.path.join(BASE_DIR,'static') ]
建立app
python3 manage.py startapp app1 #建立名爲app1的app
urls.py 文件和 建立app裏的views.py文件
#views文件 from django.shortcuts import render,HttpResponse #添加一個HttpResponse模塊 # Create your views here. def index(request): #增長函數 必須傳入一個參數,公認使用request return HttpResponse('<h1>hehehe<h1>') #return HttpResponse對象 返回裏面的內容到頁面
#urls.py 文件 from django.conf.urls import url from django.contrib import admin from app1 import views #此處引用app1裏的 views 文件 urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/$', views.index), #url尋址 並將views文件名爲參數 和 index函數名爲參數傳入,尖角號表示開頭以index開頭 ]
urls.py
from django.conf.urls import url from django.contrib import admin from app1 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^index/', views.index), url(r'^login/$',views.login), #新加一個登陸尋址 ^和$ 表示正則匹配必須是以login/開頭和結尾 ]
views.py
#render 返回一個html文件 HttpRespinse 返回一個內容 redirect重定向到一個網頁
from django.shortcuts import render,HttpResponse,redirect # Create your views here. def index(request): return HttpResponse('<h1>hehehe<h1>') def login(request): # request.method 發送的請求是什麼,若是是get請求則request.method = GET if request.method == 'POST': #判斷是不是post請求 print(request.POST) #發送請求的內容是一個查詢字典 <QueryDict: {'user': ['www'], 'pwd': ['kkk']}> user = request.POST.get('user') #獲取網頁傳過來的參數 passwd = request.POST.get('pwd') if user == 'wk' and passwd == '123': return redirect('/index/') #重定向跳轉地址能夠寫外部的(https://www.baidu.com/) return render(request,'login.html')
html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="" method="post"> 建立一個表單 action="" 向自己提交 <p> 搜索:<input type="text" name='user'> </p> <p> 密碼:<input type="password" name='pwd'> </p> <button>提交</button> </form> </body> </html>
扒一個jQuery上的頁面作html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登陸界面</title> <link rel="stylesheet" href="/static/css/reset.css"> <link rel="stylesheet" href="/static/css/style.css"> </head> {#<body>#} {#<form action="" method="post"> {#建立一個表單 action="" 向自己提交#} {# <p>#} {# 搜索:<input type="text" name='user'>#} {# </p>#} {# <p>#} {# 密碼:<input type="password" name='pwd'>#} {# </p>#} {# {{ err }}#} {# <button>提交</button>#} {#</form>#} {#</body>#} <body> <div id="particles-js"> <div class="login"> <div class="login-top"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;"> 登陸 </font></font></div> <form action=""></form> <form action="" method="post"> <div class="login-center clearfix"> <div class="login-center-img"><img src="/static/imgs/name.png"></div> <div class="login-center-input"> <input type="text" name="user" value="admin" placeholder="請輸入您的用戶名" onfocus="this.placeholder=''" onblur="this.placeholder='請輸入您的用戶名'"> <div class="login-center-input-text"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">用戶名</font></font></div> </div> </div> <div class="login-center clearfix"> <div class="login-center-img"><img src="/static/imgs/password.png"></div> <div class="login-center-input"> <input type="password" name="pwd" value="" placeholder="請輸入您的密碼" onfocus="this.placeholder=''" onblur="this.placeholder='請輸入您的密碼'"> <div class="login-center-input-text"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">密碼</font></font></div> </div> </div> <div style="text-align: center">{{ err }}</div> <div style="text-align: center"> <button class="login-button" >登錄</button> </div> </form> </div> <div class="sk-rotating-plane"></div> <canvas class="particles-js-canvas-el" width="1199" height="273" style="width: 100%; height: 100%;"></canvas> </div> <script src="/static/js/particles.min.js"></script> <script src="/static/js/app.js"></script> <script type="text/javascript"> function hasClass(elem, cls) { cls = cls || ''; if (cls.replace(/\s/g, '').length == 0) return false; //當cls沒有參數時,返回false return new RegExp(' ' + cls + ' ').test(' ' + elem.className + ' '); } function addClass(ele, cls) { if (!hasClass(ele, cls)) { ele.className = ele.className == '' ? cls : ele.className + ' ' + cls; } } function removeClass(ele, cls) { if (hasClass(ele, cls)) { var newClass = ' ' + ele.className.replace(/[\t\r\n]/g, '') + ' '; while (newClass.indexOf(' ' + cls + ' ') >= 0) { newClass = newClass.replace(' ' + cls + ' ', ' '); } ele.className = newClass.replace(/^\s+|\s+$/g, ''); } } document.querySelector(".login-button").onclick = function () { addClass(document.querySelector(".login"), "active") setTimeout(function () { addClass(document.querySelector(".sk-rotating-plane"), "active") document.querySelector(".login").style.display = "none" }, 800) setTimeout(function () { removeClass(document.querySelector(".login"), "active") removeClass(document.querySelector(".sk-rotating-plane"), "active") document.querySelector(".login").style.display = "block" alert("登陸成功") }, 5000) } </script> <div id="goog-gt-tt" class="skiptranslate" dir="ltr"> <div style="padding: 8px;"> <div> <div class="logo"><img src="https://www.gstatic.com/images/branding/product/1x/translate_24dp.png" width="20" height="20" alt="Google 翻譯"></div> </div> </div> <div class="top" style="padding: 8px; float: left; width: 100%;"><h1 class="title gray">原文</h1></div> <div class="middle" style="padding: 8px;"> <div class="original-text"></div> </div> <div class="bottom" style="padding: 8px;"> <div class="activity-links"><span class="activity-link">提供更好的翻譯建議</span><span class="activity-link"></span> </div> <div class="started-activity-container"> <hr style="color: #CCC; background-color: #CCC; height: 1px; border: none;"> <div class="activity-root"></div> </div> </div> <div class="status-message" style="display: none;"></div> </div> <div class="goog-te-spinner-pos"> <div class="goog-te-spinner-animation"> <svg xmlns="http://www.w3.org/2000/svg" class="goog-te-spinner" width="96px" height="96px" viewBox="0 0 66 66"> <circle class="goog-te-spinner-path" fill="none" stroke-width="6" stroke-linecap="round" cx="33" cy="33" r="30"></circle> </svg> </div> </div> </body> </html>
python 類 對應 mysql 表
對象 數據行(記錄)
屬性 字段
1.操做數據表 (不能操做數據庫)
2.操做具體數據
1.建立一個mysql數據庫
mysql> create databases pyku;
2.修改settings配置文件鏈接數據庫
DATABASES = { #數據庫的配置 'default': { 'ENGINE': 'django.db.backends.mysql', #將sqlite3修改成mysql 'NAME': 'pyku', #庫名 'HOST': '39.96.90.45', #基本設置 'PORT': 3306, 'USER': 'root', 'PASSWORD' : '123456' } }
3.告訴django使用pymysql模塊鏈接數據庫,在項目settings配置文件同級的__init__方法裏添加
import pymysql #在__init__文件里加入pymysql模塊 pymysql.install_as_MySQLdb() #把默認模塊MySQLdb替換爲pymysql
4.在app下的models.py中寫類,並繼承models.Model
from django.db import models class User(models.Model): #建立一個類繼承models.Model name = models.CharField(max_length=32) #等同於varchar(32) pwd = models.CharField(max_length=32)
5.執行數據庫遷移的命令
python3 manage.py makemigrations #建立數據庫遷移文件,保存每一個app下models的變動記錄 python3 manage.py migrate #將models的變動記錄同步到數據庫
6.在數據庫插入數據
7.判斷收到的數據是否在數據庫
#render 返回一個html文件 HttpRespinse 返回一個內容 redirect重定向到一個網頁 from django.shortcuts import render,HttpResponse,redirect from app1 import models #引用app項目裏的數據庫文件 # Create your views here. def index(request): return HttpResponse('<h1>hehehe<h1>') def login(request): # request.method 發送的請求是什麼,若是是get請求則request.method = GET if request.method == 'POST': #判斷是不是post請求 print(request.POST) #發送請求的內容是一個查詢字典 <QueryDict: {'user': ['www'], 'pwd': ['kkk']}> user = request.POST.get('user') #獲取網頁傳過來的參數 passwd = request.POST.get('pwd') if models.User.objects.filter(name=user,pwd=passwd): #判斷收到的數據是否在數據庫 return redirect('/index/') #重定向跳轉地址能夠寫外部的(https://www.baidu.com/) else: err_msg = '用戶名或密碼錯誤' return render(request,'login.html',{'err': err_msg})
1.models.py中建立數據表
from django.db import models class User(models.Model): #建立一個類繼承models.Model name = models.CharField(max_length=32) #等同於varchar(32) pwd = models.CharField(max_length=32) #出版社 class chubanshe(models.Model): name = models.CharField(max_length=32)
2.執行數據庫遷移的命令
python3 manage.py makemigrations #建立數據庫遷移文件,保存每一個app下models的變動記錄 python3 manage.py migrate #將models的變動記錄同步到數據庫
3.添加數據
4.添加url
urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^login/',views.login), url(r'^zs/',views.zhanshi) ]
5.後臺處理頁面展現內容
#render 返回一個html文件 HttpRespinse 返回一個內容 redirect重定向到一個網頁 from django.shortcuts import render,HttpResponse,redirect from app1 import models #引用數據庫類def zhanshi(request): # 從數據庫查詢全部的出版社 all_chubanshe = models.chubanshe.objects.all() #獲取chubanshe表的全部數據 return render(request,'zhanshi.html',{'all_chubanshe': all_chubanshe}) #將全部數據當作變量傳給前端
6.前端頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table> <thead> <tr> <th>id</th> <th>名稱</th> </tr> </thead> <tbody> {% for i in all_chubanshe %} {# for循環取後臺傳遞的數據 #} <tr> <td>{{ i.id }}</td> {# 定義變量展現數據庫對應的值 #} <td>{{ i.name }}</td> </tr> {% endfor %} </tbody> </table> </body> </html>
7.顯示結果
1.添加url
from django.conf.urls import url from django.contrib import admin from app1 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^login/',views.login), url(r'^zs/',views.zhanshi), url(r'^tj/',views.tianjia) ]
2.添加url對應的views函數
#render 返回一個html文件 HttpRespinse 返回一個內容 redirect重定向到一個網頁 from django.shortcuts import render,HttpResponse,redirect from app1 import models #引用數據庫類 # Create your views here. def login(request): # request.method 發送的請求是什麼,若是是get請求則request.method = GET err_msg = '' if request.method == 'POST': #判斷是不是post請求 print(request.POST) #發送請求的內容是一個查詢字典 <QueryDict: {'user': ['www'], 'pwd': ['kkk']}> user = request.POST.get('user') #獲取網頁傳過來的參數 passwd = request.POST.get('pwd') if models.User.objects.filter(name=user,pwd=passwd): return redirect('/zs/') #重定向跳轉地址能夠寫外部的(https://www.baidu.com/) else: err_msg = '用戶名或密碼錯誤' return render(request,'login.html',{'err': err_msg}) def zhanshi(request): # 從數據庫查詢全部的出版社 all_chubanshe = models.chubanshe.objects.all() #獲取chubanshe表的全部數據 return render(request,'zhanshi.html',{'all_chubanshe': all_chubanshe}) def tianjia(request): error = '' if request.method == 'POST': # 若是是post請求提交數據,則去數據庫添加數據 #獲取前端提供的數據 cc = request.POST.get('new_name') if not cc: #判斷傳入數據是否爲空 error = '出版社名不能爲空' # 判斷數據庫有沒有該出版社 dd = models.chubanshe.objects.filter(name=cc) if dd: error = '出版社名重複' #若是既不爲空 又不重複則插入 if cc and not dd: # 鍵值插入name是數據可的name字段,cc爲前端提供的數據request.POST.get('new_name') models.chubanshe.objects.create(name=cc) return redirect('/zs/') #添加完跳轉至展現頁 return render(request,'tianjia.html',{'error': error})
3編寫對應的html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="" method="post"> <p> 出版社名稱: <input type="text" name="new_name"> </p> <div>{{ error }}</div> <button>添加</button> </form> </body> </html>
1.修改展現頁
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table> <thead> <tr> <th>序號</th> <th>id</th> <th>名稱</th> <th>操做</th> </tr> </thead> <tbody> {% for i in all_chubanshe %} {# for循環取後臺傳遞的數據 #} <tr> <td>{{ forloop.counter }}</td> {# 循環計數,循環幾回記幾回 #} <td>{{ i.id }}</td> {# 定義變量展現數據庫對應的值 #} <td>{{ i.name }}</td> <td><a href="/sc/?pk={{ i.pk }}">刪除</a></td> {# a標籤超連接 動態傳參 傳遞變量i數據的主鍵 #} </tr> {% endfor %} </tbody> </table> </body> </html>
2.增長url
from django.conf.urls import url from django.contrib import admin from app1 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^login/',views.login), url(r'^zs/',views.zhanshi), url(r'^tj/',views.tianjia), url(r'^sc/',views.Del_cbs), ]
3.寫邏輯
#render 返回一個html文件 HttpRespinse 返回一個內容 redirect重定向到一個網頁 from django.shortcuts import render,HttpResponse,redirect from app1 import models #引用數據庫類 # Create your views here. def login(request): # request.method 發送的請求是什麼,若是是get請求則request.method = GET err_msg = '' if request.method == 'POST': #判斷是不是post請求 print(request.POST) #發送請求的內容是一個查詢字典 <QueryDict: {'user': ['www'], 'pwd': ['kkk']}> user = request.POST.get('user') #獲取網頁傳過來的參數 passwd = request.POST.get('pwd') if models.User.objects.filter(name=user,pwd=passwd): return redirect('/zs/') #重定向跳轉地址能夠寫外部的(https://www.baidu.com/) else: err_msg = '用戶名或密碼錯誤' return render(request,'login.html',{'err': err_msg}) def zhanshi(request): # 從數據庫查詢全部的出版社 all_chubanshe = models.chubanshe.objects.all() #獲取chubanshe表的全部數據 return render(request,'zhanshi.html',{'all_chubanshe': all_chubanshe}) def tianjia(request): error = '' if request.method == 'POST': # 若是是post請求提交數據,則去數據庫添加數據 #獲取前端提供的數據 cc = request.POST.get('new_name') if not cc: #判斷傳入數據是否爲空 error = '出版社名不能爲空' # 判斷數據庫有沒有該出版社 dd = models.chubanshe.objects.filter(name=cc) if dd: error = '出版社名重複' #若是既不爲空 又不重複則插入 if cc and not dd: # 鍵值插入name是數據可的name字段,cc爲前端提供的數據request.POST.get('new_name') models.chubanshe.objects.create(name=cc) return redirect('/zs/') #添加完跳轉至展現頁 return render(request,'tianjia.html',{'error': error}) def Del_cbs(request): #從前端獲取要刪除對象的ID pk = request.GET.get('pk') #去數據庫進行刪除 #pk1是chubanshe表的主鍵,pk2是前段傳的信息, if models.chubanshe.objects.filter(pk=pk): #判斷數據庫有沒有前端傳來的信息 # get從庫裏查出對應的信息delete刪除(get查不到或查到多個都報錯) models.chubanshe.objects.get(pk=pk).delete() return redirect('/zs/') return HttpResponse('<p>不存在</p>')
1.修改原先展現頁
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <table> <thead> <tr> <th>序號</th> <th>id</th> <th>名稱</th> <th>操做</th> </tr> </thead> <tbody> {% for i in all_chubanshe %} {# for循環取後臺傳遞的數據 #} <tr> <td>{{ forloop.counter }}</td> {# 循環計數,循環幾回記幾回 #} <td>{{ i.id }}</td> {# 定義變量展現數據庫對應的值 #} <td>{{ i.name }}</td> <td><a href="/sc/?pk={{ i.pk }}">刪除</a></td> {# a標籤超連接 動態傳參 傳遞變量i數據的主鍵 #} <td><a href="/xiugai/?pk={{ i.pk }}">修改</a></td> </tr> {% endfor %} </tbody> </table> </body> </html>
2.添加url
from django.conf.urls import url from django.contrib import admin from app1 import views urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^login/',views.login), url(r'^zs/',views.zhanshi), url(r'^tj/',views.tianjia), url(r'^sc/',views.Del_cbs), url(r'^xiugai/',views.xg_cbs), ]
3.寫新的html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="" method="post"> <p> 出版社名稱: <input type="text" name="xg_name" value="{{ obj.name }}"> </p> <div>{{ error }}</div> <button>修改</button> </body> </html>
4.寫邏輯
#render 返回一個html文件 HttpRespinse 返回一個內容 redirect重定向到一個網頁 from django.shortcuts import render,HttpResponse,redirect from app1 import models #引用數據庫類 # Create your views here. def login(request): # request.method 發送的請求是什麼,若是是get請求則request.method = GET err_msg = '' if request.method == 'POST': #判斷是不是post請求 print(request.POST) #發送請求的內容是一個查詢字典 <QueryDict: {'user': ['www'], 'pwd': ['kkk']}> user = request.POST.get('user') #獲取網頁傳過來的參數 passwd = request.POST.get('pwd') if models.User.objects.filter(name=user,pwd=passwd): return redirect('/zs/') #重定向跳轉地址能夠寫外部的(https://www.baidu.com/) else: err_msg = '用戶名或密碼錯誤' return render(request,'login.html',{'err': err_msg}) def zhanshi(request): # 從數據庫查詢全部的出版社 all_chubanshe = models.chubanshe.objects.all() #獲取chubanshe表的全部數據 return render(request,'zhanshi.html',{'all_chubanshe': all_chubanshe}) def tianjia(request): error = '' if request.method == 'POST': # 若是是post請求提交數據,則去數據庫添加數據 #獲取前端提供的數據 cc = request.POST.get('new_name') if not cc: #判斷傳入數據是否爲空 error = '出版社名不能爲空' # 判斷數據庫有沒有該出版社 dd = models.chubanshe.objects.filter(name=cc) if dd: error = '出版社名重複' #若是既不爲空 又不重複則插入 if cc and not dd: # 鍵值插入name是數據可的name字段,cc爲前端提供的數據request.POST.get('new_name') models.chubanshe.objects.create(name=cc) return redirect('/zs/') #添加完跳轉至展現頁 return render(request,'tianjia.html',{'error': error}) def Del_cbs(request): #從前端獲取要刪除對象的ID pk = request.GET.get('pk') #去數據庫進行刪除 #pk1是chubanshe表的主鍵,pk2是前段傳的信息, if models.chubanshe.objects.filter(pk=pk): #判斷數據庫有沒有前端傳來的信息 # get從庫裏查出對應的信息delete刪除(get查不到或查到多個都報錯) models.chubanshe.objects.get(pk=pk).delete() return redirect('/zs/') return HttpResponse('<p>不存在</p>') def xg_cbs(request): error = '' pk = request.GET.get('pk') #回去前端請求時發過來的參數 obj = models.chubanshe.objects.get(pk=pk) #根據主鍵數據庫查出對應的值 if request.method == 'POST': xg_name = request.POST.get('xg_name') if not xg_name: error = '不能爲空' dd = models.chubanshe.objects.filter(name=xg_name) if dd : error = '在數據庫已存在' if xg_name and not dd: obj.name = xg_name #修改數據庫對象的name爲新的值 obj.save() #提交上去纔會修改 return redirect('/zs/') return render(request,'xiugai.html',{'obj':obj,'error': error}) #將對應信息 傳到前端