2

Guys help solve a small problem. In my project I upload the images. When I upload the images from the admin panel, everything works perfectly. Now I need to upload the images from the form. Form excellent upload every field in the table, except for the image. My source code:

forms.py:

from django import forms


class EditorForm(forms.Form):
    title = forms.CharField(label='Title', widget=forms.TextInput(attrs={
        'class': 'form-control', 'type': 'text', 'name': 'title',
        'id': 'title', 'placeholder': 'title here'}))
    description = forms.CharField(label='Description', widget=forms.Textarea(
        attrs={'class': 'form-control', 'name': 'description',
               'id': 'description', 'placeholder': 'Long description here'}))
    short_description = forms.CharField(
        label='Short description', max_length=160, widget=forms.TextInput(
            attrs={'class': 'form-control', 'name': 'short', 'id': 'short',
                   'placeholder': 'Short description'}))
    coordinates = forms.CharField(
        label='Coordinates', required=False, widget=forms.TextInput(attrs={
            'class': 'form-control', 'type': 'text', 'name': 'latlng',
            'id': 'latlng', 'readonly': 'readonly'}))
    # img = forms.ImageField()

views.py:

class PointEditorView(FormView):
    template_name = 'geo_location/editor.html'
    form_class = EditorForm
    success_url = '/point/'

    def form_valid(self, form):
        # I DO IT BUT I NEED SOME TESTS!
        # This method is called when valid form data has been POSTed.
        # It should return an HttpResponse.
        if form.is_valid():
            Point.objects.create(
                title=form.cleaned_data['title'],
                description=form.cleaned_data['description'],
                coordinate=form.cleaned_data['coordinates'],
                short_description=form.cleaned_data['short_description'],
                # img=form.cleaned_data['img']
            )
        return super(PointEditorView, self).form_valid(form)

file.html

    <form action="" method="post" class="form-horizontal" role="form">{% csrf_token %}
        <legend>Добавляем точку</legend>
        <fieldset>
            <div class="form-group">
            {{ form.as_p }}
<br/>
            <div class="row">
              <div class="col-md-12">
                <button type="submit" class="btn btn-success">Сохранить</button>
              </div>
            </div>

models.py:

class Point(models.Model):
    title = models.CharField(max_length=32)
    short_description = models.CharField(max_length=120)
    description = models.TextField()
    coordinate = models.PointField(srid=4326)
    objects = models.GeoManager()
    img = ThumbnailImageField(upload_to='photos', blank=True)
2
  • You have ImageField commented. Is this intended or just a typo? Commented Nov 12, 2013 at 18:05
  • He is commented out in order to avoid errors Commented Nov 12, 2013 at 18:08

1 Answer 1

2

You need to specify enctype="multipart/form-data" on form

<form action="" method="post" class="form-horizontal" role="form" enctype="multipart/form-data">
Sign up to request clarification or add additional context in comments.

2 Comments

Nice catch, I've forget about that! :)
@PauloBu I was actually creating a form with files 5 mins before I answered this question. So it is fresh in my mind :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.