SQL儲存過程
阿新 • • 發佈:2022-05-08
目錄
django入門三件套
HttpResponse
主要用於返回字串型別的資料
render
主要用於返回html檔案並支援模板語法(django自帶)
redirect
主要用於重定向,括號內可以是其他網站的全稱,也可是自己網站的字尾
靜態檔案及相關配置
編寫完成後不會經常修改的與html頁面相關的檔案
css檔案、js檔案、圖片檔案、第三方框架檔案(bootstrap)等檔案都可以稱之為'靜態檔案'
在django中為靜態檔案單獨開設一個資料夾儲存 ,預設取名static資料夾
在static資料夾內建議根據功能的不同繼續劃分不同的檔案
css資料夾 js資料夾 img資料夾 others資料夾
request物件方法
# 提交post請求,預設會報403 前期不需要過多考慮,直接去配置檔案中註釋一行即可 MIDDLEWARE = [ # 'django.middleware.csrf.CsrfViewMiddleware', ] # 一、get請求和post請求都會觸發同一個檢視函式login的執行 如何針對不同的請求執行不同的程式碼 get請求返回一個登入頁面 post請求獲取使用者資料並校驗 # 二、獲取當前請求方式 request.method 返回的是純大寫的請求方法字串 if request.method == 'POST': return HttpResponse('傳送post請求成功') return render(request, 'login.html') # 預設讓檢視函式處理get請求 # 三、獲取post請求提交的普通資料 request.POST 返回結果是一個QueryDict request.POST.get('username') request.POST.getlist('hobby') 注意:get方法會拿到值列表中最後一個元素,而不是整個列表 getlist方法會直接拿到整個值列表 # 四、如何獲取url後面攜帶的資料 request.GET 結果是一個QueryDict 可以看成字典處理 request.GET.get('info') request.GET.getlist('cityList')
案例
view.py
from django.shortcuts import render, HttpResponse, redirect # Create your views here. def login(request): print(request.method) if request.method == 'POST': username = request.POST.get('username') password = request.POST.get('password') print(username, password) return HttpResponse('提交成功') return render(request, 'login.html') def index(request): return render(request, 'index.html') def home(request): return HttpResponse('<br><h1 style="text-align:center">歡迎來到主介面</h1>') def go_home(request): return redirect('/home/')
login.html
from django.shortcuts import render, HttpResponse, redirect
# Create your views here.
def register(request):
print(request.method)
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
confirm_password = request.POST.get('confirm_password')
print(username, password)
if password == confirm_password:
return HttpResponse('提交成功')
else:
return HttpResponse('前後密碼不一致')
return render(request, 'register.html')
def index(request):
return render(request, 'index.html')
def home(request):
return HttpResponse('<br><h1 style="text-align:center">歡迎來到主介面</h1>')
def go_home(request):
return redirect('/home/')
pycharm連結MySQL
django連結MySQL
django orm操作