0

我在 models.py 和 form.py 中创建了一个配置文件表单来更新它,但是除了配置文件图片 views.py之外所有的东西都得到了更新

视图.py

@login_required
def update_profile(request):
    if request.method == 'POST':
        profile_form = ProfileForm(request.POST, request.FILES, instance=request.user.profile)
        if profile_form.is_valid():
            profile_form.save()
            messages.success(request, "Your profile updated.")
        else:
            status_code = 400
            message = 'Please correct the error below.'
            messages.error(request, "Please use correct information")
    else:
        profile_form = ProfileForm(instance=request.user.profile)
    return render(request, 'profile.html', {
        'profile_form': profile_form
    })

表格.py

模型.py

4

2 回答 2

0

try using forward slash at the end of upload_to path like this: upload_to='profile_pics/'

于 2021-01-30T19:02:20.413 回答
0

首先,您需要在请求中包含request.FILES初始化表单POST的位置,如下所示:

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

然后在您的html中,您需要向表单enctype="multipart/form-data"添加属性:

<form method="post" enctype="multipart/form-data">
    {% csrf_token %}
    {{ profile_form.as_p }}
    <button type="submit" value="Submit"></button>
</form>
于 2021-01-30T19:45:40.347 回答