文件上传,restful示例
from django.conf import settings
from django.shortcuts import HttpResponse, render, redirect
import json
# Create your views here.
def img(request):
f = request.FILES.get('file',None)
fname=settings.MEDIA_ROOT + "/img/" + f.name
with open(fname, 'wb') as pic:
for c in f.chunks():
pic.write(c)
# print(f)
src=dict(src=f.name)
res=dict(code=0, msg='',data=src)
return HttpResponse(json.dumps(res))
前端模板示例
{% extends '../window/layui_common.html' %}
{% block page-main %}
<ul class="layui-timeline">
{% for i in list %}
<li class="layui-timeline-item">
<i class="layui-icon layui-timeline-axis"></i>
<div class="layui-timeline-content layui-text">
<div class="layui-timeline-title">{{ i.time }}</div>
<div>{{ i.desc }}</div>
{% if i.img %}
<div><img class="img-ani" src="/static/media/img/{{ i.img }}" width="200px" alt=""></div>
{% else %}
{% endif %}
</div>
</li>
{% endfor %}
<style>
.img-ani:hover{
width: 100%;
}
</style>
</ul>
{% endblock %}