Python 解压用户通过Django中的模型上传的文件
用户可以通过models.py上传zip文件,但我无法解压该文件以遍历图像并在模板中显示它们 Models.py views.py Post-create和index-view有效/或至少有效,直到我从上传单个图像改为上传zip文件并尝试迭代Python 解压用户通过Django中的模型上传的文件,python,django,zip,Python,Django,Zip,用户可以通过models.py上传zip文件,但我无法解压该文件以遍历图像并在模板中显示它们 Models.py views.py Post-create和index-view有效/或至少有效,直到我从上传单个图像改为上传zip文件并尝试迭代 class IndexView(generic.ListView): template_name='photos/post.html' def get_queryset(self): return Post.objects.
class IndexView(generic.ListView):
template_name='photos/post.html'
def get_queryset(self):
return Post.objects.filter(user=self.request.user)
def DetailView(request,pk=""):
model = Post
z = zipfile.ZipFile() ### I am unsure of what goes here!!
context = {
"images": z,
}
template_name = 'photos/detail.html'
return render(request,template_name,context)
class PostCreate(generic.CreateView):
form = PostForm()
model = Post
fields = ['title','body','album_image']
if form.is_valid():
instance = form.save(commit=False)
username = form.cleaned_data['username']
album_image = form.cleaned_data['album_image']
instance.save()
context = {
"form": form,
}
detail.html
{%extends“homepage/header.html”%}
{%block content%}
{图像%中的图像为%1}
{%endif%}
{{post.title}
{{post.user}}
{{post.body}
{{post.date}
{%endblock%}
post_form.html
{%if request.user.is\u已通过身份验证%}
您好{{request.user.username}},请将文件夹上载为.zip文件
{%endif%}
{%if request.user.u经过身份验证%}
{%csrf_令牌%}
{{form.as_p}}
提交
{%else%}
您必须登录才能上载文件
{%endif%}
post.html
{%extends“homepage/header.html”%}
{%block content%}
{%if对象\列表%}
{对象列表%中相册的%
{%endfor%}
{%else%}
你没有相册!
{%endif%}
{%endblock%}
我会小心处理zip内容,可能会在这里检查循环中的文件名/类型,但下面类似的内容可能会起作用(未经测试)。基本上提取内容(假设不在zip的子文件夹中),然后Base64对其进行编码,并在src中使用该编码版本来指定数据类型
# views.py
import base64
import zipfile
def DetailView(request,pk=""):
model = Post
# z = zipfile.ZipFile() ### I am unsure of what goes here!!
mydoc = 'path_to_zip_file_here'
# probably should do some logic on the name and file type
# or be very aware of what is in the zip file
with zipfile.ZipFile(mydoc, 'r') as z:
for f in z.namelist():
images.update({f: base64.b64encode(z.read(f)),})
context = {
"images": images,
}
template_name = 'photos/detail.html'
return render(request,template_name,context)
和detail.html:
{% for name, image in images.items %}
<img src ="data:image/jpeg;base64,{{ image }}" style="width:304px;height:228px;">
{% endfor%}
{%表示名称,图像中的图像。项目%}
{%endfor%}
对不起,我是Django的新手,您如何获取zip文件的路径?它是否只是settings.py w.r.t MEDIA_URL中创建的路径?谢谢你的帮助!是的,可能。你如何处理最初的上传?你是如何展示单个图像的?这将是我怀疑的同一条路径。与上面相同的IndexView类。然后上面的def DetailView最初是类DetailView(generic.DetailView),它只包含模型和模板名称,detail.html是相同的,没有for循环,而是有,为什么在这里对上传的文件进行base64编码?使用django one将上传文件的路径保存到数据库中,而不是保存文件本身(这正是应该的方式)@LukeCollins您是对的,只需使用post.album_图像作为zipfile路径。
<html>
<body>
{% if request.user.is_authenticated%}
<h3>Hello {{request.user.username}}, please upload your folder as a .zip file</h3>
{% endif %}
<div class="container-fluid">
<div class="row">
<div class="col-sm-12 col-md-7">
<div class="panel panel-default">
<div class="panel-body">
{% if request.user.is_authenticated %}
<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>
{% else %}
<p>You must be logged in to upload a file</p>
{% endif %}
</div>
</div>
</div>
</body>
</html>
{% extends "homepage/header.html" %}
{% block content %}
{%if object_list %}
<ul>
{% for album in object_list %}
<h3><a href = "{% url 'photos:detail' album.id %}">{{album.title}} - {{album.date}}</a></h3><br>
{% endfor %}
</ul>
{% else %}
<h3>You have no photo albums!</h3>
{% endif %}
{% endblock %}
# views.py
import base64
import zipfile
def DetailView(request,pk=""):
model = Post
# z = zipfile.ZipFile() ### I am unsure of what goes here!!
mydoc = 'path_to_zip_file_here'
# probably should do some logic on the name and file type
# or be very aware of what is in the zip file
with zipfile.ZipFile(mydoc, 'r') as z:
for f in z.namelist():
images.update({f: base64.b64encode(z.read(f)),})
context = {
"images": images,
}
template_name = 'photos/detail.html'
return render(request,template_name,context)
{% for name, image in images.items %}
<img src ="data:image/jpeg;base64,{{ image }}" style="width:304px;height:228px;">
{% endfor%}