- 修改login.html
Title
- 修改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/")
- 效果图:
@进一步
- 修改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)
return render(request, 'login.html')
else:
# PUT,DELETE,HEAD,OPTION...
return redirect("/index/")
- 修改login.html
Title
-
效果图:
@ 进一步把上传文件拿到
- 修改login.html:
Title
- $
- 修改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)
# 把上传文件读取一点一点拿到
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/")
-@效果图:
-
@ 目的要求:想把所上传的文件放到统一文件里面
- 新建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/")
-
$效果图: