[PYTHON] A list of stumbling blocks in Django's image upload

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.

Implement image upload and viewing using Django

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`

The image must be removed from the fields

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!

EmployeeFilterSet resolved field 'emp_photo' with 'exact' lookup to an unrecognized field type ImageField

I forgot to add enctype = "multipart / form-data"

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.

What is enctype ='multipart / form-data'?

Recommended Posts

A list of stumbling blocks in Django's image upload
Display a list of alphabets in Python 3
Summary of stumbling blocks in installing CaboCha
Make a copy of the list in Python
Group by consecutive elements of a list in Python
Display a histogram of image brightness values in python
List of nodes in diagrams
Get the number of specific elements in a python list
Summary of stumbling blocks in Django for the first time
How to get a list of built-in exceptions in python
[python] Manage functions in a list
Try to get a list of breaking news threads in Python.
When creating a matrix in a list
Generate a list of consecutive characters
Get a list of files in a folder with python without a path
Make a list of "Houbunsha 70th Anniversary Campaign" on Selenium in Amazon
Get a list of packages installed in your current environment with python
How to send a visualization image of data created in Python to Typetalk
Generate a list packed with the number of days in the current month.
[Python] How to put any number of standard inputs in a list
[Linux] Command to get a list of commands executed in the past
I want to sort a list in the order of other lists
I made a kind of simple image processing tool in Go language.
Receive a list of the results of parallel processing in Python with starmap
How to format a list of dictionaries (or instances) well in Python
[Linux] List of Linux commands used in practice
Cut out A4 print in the image
Change the list in a for statement
Sum of variables in a mathematical model
A memorandum of understanding about django's QueryDict
[python] Get a list of instance variables
String conversion of a list containing numbers
Summary of built-in methods in Python list
Benefits of using slugfield in Django's model
[Python] Get a list of folders only
How to pass the execution result of a shell command in a list in Python
Create a Python image in Django without a dummy image file and test the image upload
How to achieve something like a list of void * (or variant) in Go?
I want to see a list of WebDAV files in the Requests module
Extract elements (using a list of indexes) in a NumPy style from a Python list / tuple
How to get a list of files in the same directory with python
Draw a graph of a quadratic function in Python
Receives and processes n objects in a list
[Python] Sort the list of pathlib.Path in natural sort
Upload a large number of images to Wordpress
How to clear tuples in a list (Python)
[python] Create a list of various character types
Get the caller of a function in Python
Display output of a list of floating point numbers
Find the number of days in a month
Rewriting elements in a loop of lists (Python)
Get only the subclass elements in a list
A proposal for versioning of features in Kedro
Get a panoramic image in Google Street View
View drug reviews using a list in Python
Get a list of IAM users with Boto3
Get a list of Qiita likes by scraping
Make a joyplot-like plot of R in python
Output in the form of a python array
Get a glimpse of machine learning in Python
Search by the value of the instance in the list