来源:python宝典 链接:
https://mp.weixin.qq.com/s/YUplCQDfAucA_rS8E1T6WA
需求
上传图片在页面显示
Form表单上传
<html lang="en"><head><meta charset="UTF-8"><title>Titletitle>head><body><form method="POST" action="/upload.html" enctype="multipart/form-data"><input type="text" name="user" /><input type="file" name="fafafa" /><input type="submit" value="提交" />form><div>
{% if img %}<img style="height: 200px;width: 200px;" src="/{
{ img }}" />
{% endif %}div><script src="/static/jquery-2.1.4.min.js">script>body>html>
//后台代码
from django.shortcuts import render
import os
def upload(request):
if request.method == 'GET':
return render(request,'upload.html')
elif request.method == "POST":
user = request.POST.get('user')
fafafa = request.POST.get('fafafa')
obj = request.FILES.get('fafafa')
file_path = os.path.join('static','upload',obj.name)
f = open(file_path, 'wb'