Django ImageField未呈现ClearableFileInput [英] Django ImageField not rendering ClearableFileInput
问题描述
开始撞墙...也许我错过了一些简单的东西.
Starting to beat my head against the wall...perhaps I am missing something simple.
models.py
class GFImage(models.Model):
image = models.ImageField(upload_to = 'uploads', null=True, blank=True)
views.py
def addImage(request):
errors = []
if request.method == 'POST':
form = ImageForm(request.POST, request.FILES)
if form.is_valid():
form.save()
urlRedirect = "/home"
return redirect(urlRedirect)
else:
form = ImageForm()
return render(request, "/add_image.html", {'form': form})
forms.py
class ImageForm(ModelForm):
class Meta:
model = GFImage
add_image.html
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<table>
{{ form.as_table }}
</table>
<input type = "submit" value = "Submit">
</form>
无论我做什么,我的表单都不会使用ClearableFileInput小部件.它应该自动默认,但是即使在表单的META中分配它也不起作用.还有什么可能阻止Django使用clearable小部件?
Whatever I do, my form will not use the ClearableFileInput widget. It should default automatically, but even assigning it in the form's META will not work. What else could be blocking Django from using the clearable widget?
推荐答案
ClearableFileInput
仅在选择了初始文件时才会显示清除复选框.查看您的表单,它看起来像是没有初始数据的新表单,因此不会显示该复选框.
The ClearableFileInput
will only display the clear checkbox when there's an initial file selected. Looking at your form, it looks like a a new form without initial data, so the checkbox won't be displayed.
def render(self, name, value, attrs=None):
.. snip ..
if value and hasattr(value, "url"):
template = self.template_with_initial
substitutions['initial'] = format_html(self.url_markup_template,
这篇关于Django ImageField未呈现ClearableFileInput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!