dert
dert

Reputation: 51

how to send a picture?

please help to solve the problem.

there is a form that has a file upload field:

<form class="profile_form" id="profile_form" action="#" method="post" enctype="multipart/form-data" novalidate>
    {% csrf_token %}    
    {{ form.avatar }}    
    <input class="btn btn-default btn_submit" type="submit" value="Сохранить изменения">
</form>

after sending the form is processed in the view:

def change_profile(request):
    entry_user_profile = UserProfile.objects.get(user_ptr_id=request.user.id)       
    form = ProfileForm(instance=entry_user_profile)

    if request.method == "POST":  
        form = ProfileForm(data=request.POST, instance=entry_user_profile)
        if form.is_valid():
            form.save()

            return HttpResponse({})   

    t = loader.get_template('page_change_profile.html')
    c = RequestContext(request, {
        'form': form, 
    }, [custom_proc])   

    return HttpResponse(t.render(c))    

but the image is not processed because it is necessary to views transfer request.FILES

please tell me how the form should look like?

PIL is installed

Upvotes: 0

Views: 69

Answers (1)

Marc Belmont
Marc Belmont

Reputation: 584

You have to pass request.FILES to ProfilesForm.

ProfileForm(request.POST, request.FILES, instance=entry_user_profile)

Upvotes: 1

Related Questions