Day19:文件上传

一、表单处理

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
    <link rel="stylesheet" href="/static/common.css"/>
    <style>
        label{width:100px;text-align:right;display: inline-block}
        input[type=submit]{margin-left: 160px}
        span{color: #ff0000}
    </style>
</head>
<body>
    <form action="/login/" method="post" enctype="multipart/form-data">
        <p>
            <label for="username">用户名:</label>
            <input id="username" name="user" type="text"/>
        </p>
        <p>
            <label for="password">密码:</label>
            <input id="username" name="pwd" type="password"/>
        </p>
        <p><input type="radio" name="sex" value="1"/><input type="radio" name="sex" value="2"/>
            licy<input type="radio" name="sex" value="3"/>
        </p>
        <p><input type="checkbox" name="favor" value="11"/><input type="checkbox" name="favor" value="22"/>
            licy<input type="checkbox" name="favor" value="33"/>
        </p>
        <p>
            <select name="city" multiple>
                <option value="bj">北京</option>
                <option value="hb">河北</option>
                <option value="zjk">张家口</option>
            </select>
        </p>
        <p>
            <input type="file" name="upload"/>
        </p>
        <p>
            <input  type="submit" value="注册"/>
            <span>{{ error_msg }}</span>
        </p>
    </form>
</body>
</html>
html文件
from django.shortcuts import render

# Create your views here.
from django.shortcuts import HttpResponse
from django.shortcuts import render
from django.shortcuts import redirect
def login(request):
    error_msg=''
    if request.method=='POST':
        #user=request.POST['user'] 如果user没有值就会报错
        user=request.POST.get('user',None)
        pwd=request.POST.get('pwd',None)
        sex=request.POST.get('sex',None)
        favor=request.POST.getlist('favor',None)
        city=request.POST.getlist('city',None)
        obj=request.FILES.get('upload')
        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()
        # if user=='licy' and pwd=='123':
        #     return redirect('/home/')
        # else:
        #     error_msg='用户名或密码错误'
    return render(request,'login.html',{'error_msg':error_msg})

USER_LIST=[
    {'username':'licy','email':'sfsfsdf','sex':''}
]

# for item in range(10):
#     temp={'username':'licy'+str(item),'email':'sfsfsdf','sex':'男'}
#     USER_LIST.append(temp)
def home(request):
    if request.method=="POST":
        username = request.POST.get('username', None)
        email = request.POST.get('email', None)
        sex = request.POST.get('sex', None)
        temp={'username':username,'email':email,'sex':sex}
        USER_LIST.append(temp)
    return render(request,'home.html',{'userlist':USER_LIST})
py文件

二、FBV(function base view)、CBV(class base view)

 

posted @ 2017-08-09 13:40  licy_python  阅读(104)  评论(0编辑  收藏  举报