image.png
- 修改login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/login/" method="post">
<p>
<input type="text" name="user" placeholder="使用者名">
</p>
<p>
<input type="password" name="pwd" placeholder="密碼">
</p>
<p>
男: <input type="radio" name="gender" value="1">
女: <input type="radio" name="gender" value="2">
卡米: <input type="radio" name="gender" value="3">
</p>
<input type="submit" value="送出">
</form>
</body>
</html>
- 修改views.py
from django.shortcuts import render,HttpResponse,redirect
# Create your views here.
def index(request):
return HttpResponse('Index')
'''
def login(request):
# 判斷使用者擷取資料方式是GET,就傳回什麼資料
if request.method == "GET":
return render(request, 'login.html')
# 判斷使用者擷取資料方式是POST,就判斷使用者送出的資料是否正确
elif request.method == "POST":
u = request.POST.get('user')
p = request.POST.get('pwd')
if u == 'alex' and p == '123':
return redirect('/index/')
else:
return render(request, 'login.html')
else:
# PUT,DELETE,HEAD,OPTION...
return redirect("/index/")
'''
def login(request):
# 判斷使用者擷取資料方式是GET,就傳回什麼資料
if request.method == "GET":
return render(request, 'login.html')
# 判斷使用者擷取資料方式是POST,就判斷使用者送出的資料是否正确
elif request.method == "POST":
v = request.POST.get('gender')
print(v)
return render(request, 'login.html')
else:
# PUT,DELETE,HEAD,OPTION...
return redirect("/index/")
- 效果圖:
@進一步
from django.shortcuts import render,HttpResponse,redirect
# Create your views here.
def index(request):
return HttpResponse('Index')
'''
def login(request):
# 判斷使用者擷取資料方式是GET,就傳回什麼資料
if request.method == "GET":
return render(request, 'login.html')
# 判斷使用者擷取資料方式是POST,就判斷使用者送出的資料是否正确
elif request.method == "POST":
u = request.POST.get('user')
p = request.POST.get('pwd')
if u == 'alex' and p == '123':
return redirect('/index/')
else:
return render(request, 'login.html')
else:
# PUT,DELETE,HEAD,OPTION...
return redirect("/index/")
'''
def login(request):
# 判斷使用者擷取資料方式是GET,就傳回什麼資料
if request.method == "GET":
return render(request, 'login.html')
# 判斷使用者擷取資料方式是POST,就判斷使用者送出的資料是否正确
elif request.method == "POST":
# radio
# v = request.POST.get('gender')
# print(v)
v = request.POST.getlist('favor')
print(v)
return render(request, 'login.html')
else:
# PUT,DELETE,HEAD,OPTION...
return redirect("/index/")
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/login/" method="post">
<p>
<input type="text" name="user" placeholder="使用者名">
</p>
<p>
<input type="password" name="pwd" placeholder="密碼">
</p>
<p>
男: <input type="radio" name="gender" value="1">
女: <input type="radio" name="gender" value="2">
卡米: <input type="radio" name="gender" value="3">
</p>
<p>
男: <input type="checkbox" name="favor" value="11">
女: <input type="checkbox" name="favor" value="22">
卡米: <input type="checkbox" name="favor" value="33">
</p>
<input type="submit" value="送出">
</form>
</body>
</html>
@ 進一步把上傳檔案拿到
- 修改login.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--# 如果要上傳檔案表單要加上enctype="multipart/form-data"-->
<form action="/login/" method="post" enctype="multipart/form-data">
<p>
<input type="text" name="user" placeholder="使用者名">
</p>
<p>
<input type="password" name="pwd" placeholder="密碼">
</p>
<p>
男: <input type="radio" name="gender" value="1">
女: <input type="radio" name="gender" value="2">
卡米: <input type="radio" name="gender" value="3">
</p>
<p>
男: <input type="checkbox" name="favor" value="11">
女: <input type="checkbox" name="favor" value="22">
卡米: <input type="checkbox" name="favor" value="33">
</p>
<p>
<select name="city" id="">
<option value="sh">上海</option>
<option value="bj">北京</option>
<option value="tj">天津</option>
</select>
</p>
<p>
<!--上傳檔案的标簽-->
<input type="file" name="fff">
</p>
<input type="submit" value="送出">
</form>
</body>
</html>
- $
from django.shortcuts import render,HttpResponse,redirect
# Create your views here.
def index(request):
return HttpResponse('Index')
'''
def login(request):
# 判斷使用者擷取資料方式是GET,就傳回什麼資料
if request.method == "GET":
return render(request, 'login.html')
# 判斷使用者擷取資料方式是POST,就判斷使用者送出的資料是否正确
elif request.method == "POST":
u = request.POST.get('user')
p = request.POST.get('pwd')
if u == 'alex' and p == '123':
return redirect('/index/')
else:
return render(request, 'login.html')
else:
# PUT,DELETE,HEAD,OPTION...
return redirect("/index/")
'''
def login(request):
# 判斷使用者擷取資料方式是GET,就傳回什麼資料
if request.method == "GET":
return render(request, 'login.html')
# 判斷使用者擷取資料方式是POST,就判斷使用者送出的資料是否正确
elif request.method == "POST":
# radio
# v = request.POST.get('gender')
# print(v)
# v = request.POST.getlist('favor')
# print(v)
v = request.POST.get('fff')
print(v)
# 所有上傳檔案都上傳到request.FILES
obj = request.FILES.get('fff')
print(obj, type(obj), obj.name)
# 把上傳檔案讀取一點一點拿到
f = open(obj.name, mode="wb")
for i in obj.chunks():
f.write(i)
f.close()
return render(request, 'login.html')
else:
# PUT,DELETE,HEAD,OPTION...
return redirect("/index/")
-@效果圖:
1.PNG
-
@ 目的要求:想把所上傳的檔案放到統一檔案裡面
- 建立upload 檔案夾
- 修改views.py:
from django.shortcuts import render,HttpResponse,redirect
# Create your views here.
def index(request):
return HttpResponse('Index')
'''
def login(request):
# 判斷使用者擷取資料方式是GET,就傳回什麼資料
if request.method == "GET":
return render(request, 'login.html')
# 判斷使用者擷取資料方式是POST,就判斷使用者送出的資料是否正确
elif request.method == "POST":
u = request.POST.get('user')
p = request.POST.get('pwd')
if u == 'alex' and p == '123':
return redirect('/index/')
else:
return render(request, 'login.html')
else:
# PUT,DELETE,HEAD,OPTION...
return redirect("/index/")
'''
def login(request):
# 判斷使用者擷取資料方式是GET,就傳回什麼資料
if request.method == "GET":
return render(request, 'login.html')
# 判斷使用者擷取資料方式是POST,就判斷使用者送出的資料是否正确
elif request.method == "POST":
# radio
# v = request.POST.get('gender')
# print(v)
# v = request.POST.getlist('favor')
# print(v)
v = request.POST.get('fff')
print(v)
# 所有上傳檔案都上傳到request.FILES
obj = request.FILES.get('fff')
print(obj, type(obj), obj.name)
# 把所上傳的檔案放到所建立的檔案夾
import os
file_path = os.path.join('upload',obj.name)
# 把上傳檔案讀取一點一點拿到
f = open(file_path, mode="wb")
for i in obj.chunks():
f.write(i)
f.close()
return render(request, 'login.html')
else:
# PUT,DELETE,HEAD,OPTION...
return redirect("/index/")
-
$效果圖: