在django中更改个人资料图片

时间:2020-02-20 06:14:20

标签: python django

我正在尝试创建一个编辑个人资料模块,但是它也需要更改个人资料图片。我已经尝试了一些代码,但是当我按下“提交”按钮时,图片没有改变

这是update_profile的views.py

def update_profile(request):
    if request.method == 'POST':
        user_form = UserInfoForm(request.POST, instance=request.user )
        profile_form = UserProfileInfoForm(request.POST, instance=request.user.profile)
        if user_form.is_valid() and profile_form.is_valid():            
            user = user_form.save()            
            user.save()
            profile = profile_form.save(commit=False)
            profile.user = user 
            if 'profile_pic' in request.FILES:                
                profile.profile_pic = request.FILES['profile_pic']           
            profile.save()         
            return redirect('/profile/')
        else:
            messages.error(request, ('Please correct the error below.'))
    else:
        user_form = UserInfoForm(instance=request.user)
        profile_form = UserProfileInfoForm(instance=request.user.profile)
    return render(request, 'profile.html', {
        'user_form': user_form,
        'profile_form': profile_form
    })

这是表格

<form role="form" class="form-horizontal" method="post">                          

                          {% load staticfiles %}
                          {% block body_block %}

                                {% if registered %}
                                  <h1>Update Profile Success!</h1>
                                {% else %}    
                                  <form class="cmxform form-horizontal style-form" id="commentForm" enctype="multipart/form-data" method="POST" action="">
                                    {% csrf_token %}        
                                    {{ user_form.as_p }}
                                    {{ profile_form.as_p }}

                                    <input type="submit" name="" value="Update">                          
                                  </form>

                                {% endif %}

                          {% endblock %}                             

                        </form>

urls.py(已经添加+ static []),来自某人的建议,但仍无法正常工作

app_name = 'polls'
urlpatterns = [
    path('profile/', views.update_profile, name='profile'),        
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

希望有人可以解决问题,谢谢。

2 个答案:

答案 0 :(得分:1)

HTML格式添加enctype

<form role="form" class="form-horizontal" method="post" enctype="multipart/form-data">
    <!-- Your html elements -->
</form>

还向您的表单提供request.FILES

profile_form = UserProfileInfoForm(request.POST, request.FILES, instance=request.user.profile)

删除这些行:

if 'profile_pic' in request.FILES:                
    profile.profile_pic = request.FILES['profile_pic']

如果要将request.FILES传递给UserProfileInfoForm,则不必直接将其分配给配置文件。

答案 1 :(得分:1)

您没有将文件传递到表单,因此将request.FILES添加为第二个参数,以在表单中包含视图中的图片,例如

profile_form = UserProfileInfoForm(request.POST, request.FILES, instance=request.user.profile