gpt4 book ai didi

python - 如何在 Django 中更新 ImageField?

转载 作者:行者123 更新时间:2023-12-04 02:32:38 27 4
gpt4 key购买 nike

我是 Django 的新手。我在更新 ImageField 时遇到问题。我有以下代码
在models.py中

 class ImageModel(models.Model):
image_name = models.CharField(max_length=50)
image_color = models.CharField(max_length=50)
image_document = models.ImageField(upload_to='product/')
- 这是我的 forms.py
 class ImageForm(forms.ModelForm):
class Meta:
model = ImageModel
fields = ['image_name', 'image_color' , 'image_document']
在 Html 文件中 (editproduct.html)
<form method="POST" action="/myapp/updateimage/{{ singleimagedata.id }}">
{% csrf_token %}
<input class="form-control" type="text" name="image_name" value="{{ singleimagedata.image_name}}">
<input class="form-control" type="file" name="image_document">
<button type="submit" class="btn btn-primary">UPDATE PRODUCT</button>
</form>
-myapp 是我的应用程序名称。 {{singleimagedata}} 是一个包含所有获取数据的变量
-urls.py
urlpatterns = [
path('productlist', views.productlist, name='productlist'),
path('addproduct', views.addproduct, name='addproduct'),
path('editimage/<int:id>', views.editimage, name='editimage'),
path('updateimage/<int:id>', views.updateimage, name='updateimage'),
]
这是我的 views.py
def productlist(request):
if request.method == 'GET':
imagedata = ImageModel.objects.all()
return render(request,"product/productlist.html",{'imagedata':imagedata})

def addproduct(request):
if request.method == 'POST':
form = ImageForm(request.POST, request.FILES)
if form.is_valid():
form.save()
messages.add_message(request, messages.SUCCESS, 'Image Uploaded')
return redirect('/myapp/productlist')
else:
imageform = ImageForm()
return render(request, "product/addproduct.html", {'imageform': imageform})

def editimage(request, id):
singleimagedata = ImageModel.objects.get(id=id)
return render(request, 'product/editproduct.html', {'singleimagedata': singleimagedata})

def updateimage(request, id): #this function is called when update data
data = ImageModel.objects.get(id=id)
form = ImageForm(request.POST,request.FILES,instance = data)
if form.is_valid():
form.save()
return redirect("/myapp/productlist")
else:
return render(request, 'demo/editproduct.html', {'singleimagedata': data})
  • 我的图像上传工作正常。更新数据时无法更新图像。其余数据已更新。我不知道如何更新图像以及如何删除旧图像并将新图像放入目录。
  • 最佳答案

    我想你错过了 enctype="multipart/form-data" ,尝试改变:

    <form method="POST" action="/myapp/updateimage/{{ singleimagedata.id }}">
    进入;
    <form method="POST" enctype="multipart/form-data" action="{% url 'updateimage' id=singleimagedata.id %}">

    Don't miss also to add the image_color field to your html input.
    Because, in your case the image_color field model is designed as required field.


    从目录中删除和更新旧图像文件;
    import os
    from django.conf import settings

    # your imported module...


    def updateimage(request, id): #this function is called when update data
    old_image = ImageModel.objects.get(id=id)
    form = ImageForm(request.POST, request.FILES, instance=old_image)

    if form.is_valid():

    # deleting old uploaded image.
    image_path = old_image.image_document.path
    if os.path.exists(image_path):
    os.remove(image_path)

    # the `form.save` will also update your newest image & path.
    form.save()
    return redirect("/myapp/productlist")
    else:
    context = {'singleimagedata': old_image, 'form': form}
    return render(request, 'demo/editproduct.html', context)

    关于python - 如何在 Django 中更新 ImageField?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63298721/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com