【问题标题】:how to send a picture?如何发送图片?
【发布时间】:2014-09-20 13:48:14
【问题描述】:

请帮助解决问题。

有一个表单有一个文件上传字段:

<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>

发送表单后在视图中处理:

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))    

但是图像没有被处理,因为需要查看传输请求.FILES

请告诉我表格应该是什么样子的?

PIL 已安装

【问题讨论】:

  • 这是什么? return HttpResponse({})? [custom_proc] 是什么?您可以将方法的最后三行替换为 return render(request, 'page_change_profile.html', {'form': form})

标签: python django python-3.x django-forms


【解决方案1】:

您必须将request.FILES 传递给ProfilesForm

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

【讨论】:

    猜你喜欢
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-11
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    相关资源
    最近更新 更多