Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 即使在使用DJANGO模型表单提交后,数据库中的图片也不会得到更新_Python_Django_Django Models - Fatal编程技术网

Python 即使在使用DJANGO模型表单提交后,数据库中的图片也不会得到更新

Python 即使在使用DJANGO模型表单提交后,数据库中的图片也不会得到更新,python,django,django-models,Python,Django,Django Models,我打算用django中的个人资料图片创建一个用户个人资料模型 一切正常,但我无法更新用户的个人资料图片 表单显示在网站上,但单击“提交”后,数据库中看不到任何更改,图像也不会上载 下面是我的HTML表单代码 <form method="POST" action="{% url 'profile' %}" enctype="multipart/form-data"> {% csrf_token %}

我打算用django中的个人资料图片创建一个用户个人资料模型

一切正常,但我无法更新用户的个人资料图片

表单显示在网站上,但单击“提交”后,数据库中看不到任何更改,图像也不会上载

下面是我的HTML表单代码

<form method="POST" action="{% url 'profile' %}" enctype="multipart/form-data">
                                {% csrf_token %}
                                {{ form.as_p }}

                                  <br><br>
                                <div class="mb-3"><button class="btn btn-primary btn-sm" name="_picture" type="submit">Change Photo</button></div>
                            </form>
下面是views.py的代码

    @login_required
   def profile(request):

    current_user_profile = UserProfileInfo.objects.get(user=request.user)
    current_user = request.user

    form = UpdateProfilePic(instance=current_user_profile)

    context = {'current_user':current_user,'current_user_profile':current_user_profile,'form':form}
    if request.method=='POST':

        if '_user' in request.POST:
            temp_user = request.user
            temp_user.username = request.POST.get('username')
            temp_user.email = request.POST.get('email')
            temp_user.first_name = request.POST.get('first_name')
            temp_user.last_name = request.POST.get('last_name')
            temp_user.save()
            context = {'current_user':current_user,'current_user_profile':current_user_profile,'form':form}
            return render(request,'instructor_app/profile.html',context)

        if '_profile' in request.POST:
            temp_user_profile = UserProfileInfo.objects.get(user=request.user)
            temp_user_profile.address = request.POST.get('address')
            temp_user_profile.city = request.POST.get('city')
            temp_user_profile.pincode = request.POST.get('pincode')
            temp_user_profile.save()
            context = {'current_user':current_user,'current_user_profile':current_user_profile,'form':form}
            return render(request,'instructor_app/profile.html',context)

        if '_picture' in request.POST:
            print("in picture")
            form = UpdateProfilePic(request.POST, instance=UserProfileInfo.objects.get(user=request.user))
            form.save()
            context = {'current_user':current_user,'current_user_profile':current_user_profile,'form':form}
            return render(request,'instructor_app/profile.html',context)
    else:
        return render(request,'instructor_app/profile.html',context)
if '_picture' in request.POST:
            form = UpdateProfilePic(request.POST, request.FILES)
            if form.is_valid():
                m = UserProfileInfo.objects.get(user=request.user)
                m.profile_picture = form.cleaned_data['image']
                m.save()
                return redirect('/profile')

请帮我查一下密码

您可以通过如下所示创建模型表单或简单的Django表单,并使用表单的action属性传递文件来编辑模型

您可以尝试以下方法:

form.py

from django import forms


class UpdateProfilePic(forms.Form):
    image = forms.ImageField()
在您的视图中.py

    @login_required
   def profile(request):

    current_user_profile = UserProfileInfo.objects.get(user=request.user)
    current_user = request.user

    form = UpdateProfilePic(instance=current_user_profile)

    context = {'current_user':current_user,'current_user_profile':current_user_profile,'form':form}
    if request.method=='POST':

        if '_user' in request.POST:
            temp_user = request.user
            temp_user.username = request.POST.get('username')
            temp_user.email = request.POST.get('email')
            temp_user.first_name = request.POST.get('first_name')
            temp_user.last_name = request.POST.get('last_name')
            temp_user.save()
            context = {'current_user':current_user,'current_user_profile':current_user_profile,'form':form}
            return render(request,'instructor_app/profile.html',context)

        if '_profile' in request.POST:
            temp_user_profile = UserProfileInfo.objects.get(user=request.user)
            temp_user_profile.address = request.POST.get('address')
            temp_user_profile.city = request.POST.get('city')
            temp_user_profile.pincode = request.POST.get('pincode')
            temp_user_profile.save()
            context = {'current_user':current_user,'current_user_profile':current_user_profile,'form':form}
            return render(request,'instructor_app/profile.html',context)

        if '_picture' in request.POST:
            print("in picture")
            form = UpdateProfilePic(request.POST, instance=UserProfileInfo.objects.get(user=request.user))
            form.save()
            context = {'current_user':current_user,'current_user_profile':current_user_profile,'form':form}
            return render(request,'instructor_app/profile.html',context)
    else:
        return render(request,'instructor_app/profile.html',context)
if '_picture' in request.POST:
            form = UpdateProfilePic(request.POST, request.FILES)
            if form.is_valid():
                m = UserProfileInfo.objects.get(user=request.user)
                m.profile_picture = form.cleaned_data['image']
                m.save()
                return redirect('/profile')
还有你的HTML文件

<form action="{% url 'profile' %}" method="POST" enctype="multipart/form-data">
                                    {% csrf_token %}
                                    <div class="input-group">
                                        <div class="custom-file">
                                          <input type="file" class="custom-file-input" id="inputGroupFile04" aria-describedby="inputGroupFileAddon04" name="image">
                                          <label class="custom-file-label" for="inputGroupFile04">Choose file</label>
                                        </div>
                                        <div class="input-group-append">
                                          <button class="btn btn-primary" type="submit" name="_picture" id="inputGroupFileAddon04">Upload</button>
                                        </div>
                                      </div>

                                </form>

{%csrf_令牌%}
选择文件
上传

我认为您的views.py不完整。你能用fullviews.py更新你的代码吗