Can't submit picture through generic view form

8 views
Skip to first unread message

Michael Ackerman

unread,
May 19, 2012, 10:55:11 AM5/19/12
to django...@googlegroups.com
I have a generic view:

class create_ticket(CreateView):
    model = ticket
    form_class = ticket_form
    template_name = "create_ticket.html"
    success_url = "/tickets/thanks/"

and a form:

class ticket_form(ModelForm):
    class Meta:
        model = ticket
        fields = ('title','description','picture')

But when I try to submit the data, it get a "This field is required" for the picture, so I think I'm missing something in order to take in the picture.

and for reference:

#create_ticket.html:
{% extends "base.html" %}
{% block main %}
<form action="" method="post">{% csrf_token %}
    {{ form.as_table}}
<input type="submit" value="Submit" />
</form>
{% endblock %}

#models.py
class ticket(models.Model):
    title = models.CharField(max_length = 100)
    date_created = models.DateTimeField(auto_now_add=True)
    description = models.TextField()
    ranking = models.PositiveIntegerField(default=0)
    picture = models.ImageField(
        upload_to ='pictures' )

    def __unicode__(self):
        return self.title

All help is appreciated, thank you.

Jian Chang

unread,
May 20, 2012, 2:38:49 AM5/20/12
to django...@googlegroups.com

Your form need a attribute named enctype, and the value is multipart/form-data, try it.

> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply all
Reply to author
Forward
0 new messages