liveBook · Manning

Enctype attribute

As explained in Django in Action

enctype is an HTML form attribute that specifies the encoding used for submitted form data.

The HTML form enctype attribute chooses how submitted form data is encoded. For file uploads, set enctype="multipart/form-data"; otherwise the selected file will not reach the server, creating a particularly sneaky bug. section 7.4.3

Uploading a venue picture

A venue form can submit its name, description, and picture with <form enctype="multipart/form-data" method="post">. The Django view then binds request.POST and request.FILES to the form, allowing the uploaded picture to be saved with the Venue. section 7.4.3

Listing 7.19 Template for adding or editing a Venue object
<!-- RiffMates/templates/edit_venue.html -->
{% extends "base.html" %}

{% block content %}
<h1>Venue Details</h1>

<form enctype="multipart/form-data" method="post"> #1
  {% csrf_token %}
  {{form.as_p}}
  <button class="btn btn-dark" type="submit">Save</button>
</form>

{% endblock content %}

See the complete form markup, including multipart encoding, CSRF protection, rendered fields, and the Save button. section 7.4.3

How Django in Action covers it

How deep it goes
Explains the required attribute value and its failure mode, then shows the form markup and Django request handling.
Examples
Uses a venue form that uploads a picture while saving its name and description.
What it assumes
Assumes a working HTML form and Django view that process submitted fields and uploaded files.
What you can do after
Apply it via HTML and Django to make file-upload forms submit successfully.
How it connects
Connects HTML forms with file uploads, Django forms, request.POST, and request.FILES.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.