ssapp
ssapp

Reputation: 323

Django FileField, unable to upload

I have Class:

class Course(models.Model):
    name = models.CharField(
        max_length=255,
        )
    upload = models.FileField(upload_to='courses/')

And when user is adding new course it prompts me to enter title and to choose file.. After i enter title and choose file, filename appears. When i press Save button, file name dissapears and i get message "This field is required" Here is my template:

<form action="{% url "courses-new" %}" method="POST">
  {% csrf_token %}
  <ul>
    {{ form.as_ul }}
  </ul>
  <input id="save_course" type="submit" value="Save" />
</form>

<a href="{% url "courses-list" %}">back to list</a>

Upvotes: 0

Views: 714

Answers (1)

v1k45
v1k45

Reputation: 8250

You should set your form enctype as multipart/form-data. Like this:

<form action="{% url "courses-new" %}" method="POST" enctype="multipart/form-data">

From django-docs:

Note that request.FILES will only contain data if the request method was POST and the that posted the request has the attribute enctype="multipart/form-data". Otherwise, request.FILES will be empty.

Upvotes: 3

Related Questions