我正在尝试上传.zip存档中的图像以及与.xlsx相关的数据。
这是我试图导入图像的admin.py文件
@admin.register(ImageModel)
class ImageAdmin(admin.ModelAdmin):
change_list_template = "entities/images_changelist.html"
list_display = ('name', 'img')
@staticmethod
def img(obj):
return mark_safe(
f'<img src="{obj.image.url}" height="100">') if obj.image else None
def get_urls(self):
urls = super().get_urls()
my_urls = [
path('import-images/', self.import_images),
]
return my_urls + urls
def import_images(self, request):
form = forms.ImagesUploadForm()
if request.method == "POST":
...Parsing them...
self.message_user(request, "Your images have been imported")
return redirect('/admin/images/imagemodel/')
return render(request, "admin/images_form.html", {"form": form})
这是html的形式: {%extends'admin / base.html'%}
{% block content %}
<div>
<form action="." method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Import images</button>
</form>
</div>
{% endblock %}
这是我的表格:
class ImagesUploadForm(forms.Form):
excel_file = forms.FileField()
zip_file = forms.FileField()
它在我的本地计算机上运行良好,但我在开发服务器上遇到400错误。另外,当我添加if form.is_valid():
时,我总是得到False
和form.errors == ''