I was trying to add an image upload function in Django, but it took a long time unexpectedly, so I will note the points to reflect on.
I implemented image upload while referring to this article.
The main mistakes are:
--Since I was copying the code of other qiita articles due to brain death, the variable names were usually different. ――I didn't read every corner of the qiita article, so I overlooked several things.
The former is usually an individual issue, so I will talk about what the latter overlooked.
environment: Mac OS Mojave python 3.7 django 2.1.2 Pillow 7.1.2
This project name: ʻapp`
When I was copying and pasting, I got the following error on the command line.
resolved field 'emp_photo' with 'exact' lookup to an unrecognized field type ImageField
After a lot of research, I found that I had to specify fields
in ʻapp / filters.py to exclude variables in the image file. (ʻApp
is the name of this project.)
app/filters.py
class FooFilterSet(django_filters.FilterSet):
class Meta:
model = Foo
fields = ("hoge", "fuga") #Do not include anything related to ImageField here!
app/templates/app/form.html
<div class="row">
<div class="col-12">
<!--The following enctype="multipart/form-data"Is a must!-->
<form method="post" id="myform" enctype="multipart/form-data">
{% crispy form %}
</form>
</div>
</div>
As described above, if form method
does not include ʻenctype =" multipart / form-data "`, the contents of the file will not be sent and only the file name will be sent. More details can be found here.
Recommended Posts