我想更新我发布的帖子,它们包含标题,城市等字段和10个图像字段(图像,图像1,图像2等),每当我点击更新时,更新表单都附带所有文本字段填充了以前保存的数据,但图像字段没有得到以前提交的图像(它们显示为空)..我希望它们在那里让用户更改或删除它们等等......有人可以帮助我吗?
我的图片是使用此方法在models.py
中保存的 def get_image_filename(instance, filename):
title = instance.title
slug = slugify(title)
return "post_images/%s-%s" % (slug, filename)
这是我对update_post的看法
def update_post(request, post_id):
post_form = Posts.objects.get(pk=post_id)
if request.method == "POST":
form = AddFile(request.POST, request.FILES,
instance=post_form)
if form.is_valid():
form.title = request.POST.get('title')
form.city = request.POST.get('city')
form.price = request.POST.get('price')
form.brand = request.POST.get('brand')
form.modelyear = request.POST.get('modelyear')
form.condition = request.POST.get('condition')
form.mileage = request.POST.get('mileage')
form.details = request.POST.get('details')
form.image = request.POST.get('image')
form.image1 = request.POST.get('image1')
form.image2 = request.POST.get('image2')
form.image3 = request.POST.get('image3')
form.image4 = request.POST.get('image4')
form.image5 = request.POST.get('image5')
form.image6 = request.POST.get('image6')
form.image7 = request.POST.get('image7')
form.image8 = request.POST.get('image8')
form.image9 = request.POST.get('image9')
form.user = request.user
form.save()
messages.success(request, 'Post updated
Successfully!')
return render(request, 'vehicles_app/addfile.html',
{
'form': form,
'post_form': post_form,
'post_id': post_id,
})
else:
messages.success(request, 'Post not updated
Successfully!')
print(form.errors)
else:
form = AddFile(instance=post_form)
return render(request, 'vehicles_app/update_post.html',
{'form': form, 'post_form':post_form, 'post_id': post_id})
这是post_update.html
<form id="post_form" method="post" action="{% url
'vehicles_app:update_post' post_id %}"
enctype="multipart/form-data">
{% csrf_token %}
<div class="row">
<div class="form-group">
<div style="float: left; width: 50%">
<label for="title">Ad Title</label>
</div>
<div class="title" style="float: right; width: 45%">
<input type="text" name="title" placeholder="Type Here"
value='{{ form.initial.title }}' required>
</div>
</div>
<br>
<div class="form-group">
<div style="float: left; width: 50%">
<label for="city">City</label>
</div>
<div class="city" style="float: right; width: 45%">
<select name="city" value='{{ form.initial.city }}'>
<option selected="selected"
value="islamabad">Islamabad</option>
<option value="karachi">Karachi</option>
<option value="lahore">Lahore</option>
<option value="peshawar">Peshawar</option>
<option value="quetta">Quetta</option>
<option value="multan">Multan</option>
<option value="faisalabad">Faisalabad</option>
</select>
</div>
</div>
<br>
<div class="form-group">
<div style="float: left; width: 50%">
<label for="price">Price</label>
</div>
<div class="price" style="float: right; width: 45%">
<input type="text" name="price" placeholder="Type Here"
value='{{ form.initial.price }}' required>
</div>
</div>
<br>
<div class="form-group">
<div style="float: left; width: 50%">
<label for="brand">Brand</label>
</div>
<div class="brand" style="float: right; width: 45%">
<select name="brand" value='{{ form.initial.brand }}'>
<option selected="selected"
value="honda">Honda</option>
<option value="audi">Audi</option>
<option value="bmw">BMW</option>
<option value="suzuki">Suzuki</option>
<option value="toyota">Toyota</option>
<option value="lexus">Lexus</option>
<option value="hyundai">Hyundai</option>
<option value="jeep">Jeep</option>
<option value="mazda">Mazda</option>
<option value="mitsubishi">Mitsubishi</option>
<option value="daewoo">Daewoo</option>
</select>
</div>
</div>
<br>
<div class="form-group">
<div style="float: left; width: 50%">
<label for="modelyear">Model-Year</label>
</div>
<div class="modelyear" style="float: right; width: 45%">
<input type="text" name="modelyear"
placeholder="ie:1970-2018" value='{{
form.initial.modelyear }}' required>
</div>
</div>
<br>
<div class="form-group">
<div style="float: left; width: 50%">
<label for="condition">Condition</label>
</div>
<div class="condition" style="float: right; width: 45%">
<select name="condition" value='{{ form.initial.condition
}}'>
<option selected="selected" value="new">NEW</option>
<option value="used">USED</option>
</select>
</div>
</div>
<br>
<div class="form-group">
<div style="float: left; width: 50%">
<label for="mileage">Mileage</label>
</div>
<div class=mileage style="float: right; width: 45%">
<input type="text" name="mileage" placeholder="ie:50000"
value='{{ form.initial.mileage }}' required>
</div>
</div>
<br>
<div class="form-group">
<div style="float: left; width: 50%">
<label for="details">Type details here</label>
</div>
<div class="details" style="float: right; width: 45%">
<textarea contenteditable id="details" name="details"
rows="9" cols="45" required>{{ form.initial.details }}
</textarea>
</div>
</div>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<input type="file" name="image" value='{{
request.initial.image }}' accept="media/" required>
<input type="file" name="image1" value='{{
request.initial.image1 }}' accept="media/">
<input type="file" name="image2" value='{{
request.initial.image2 }}' accept="media/">
<input type="file" name="image3" value='{{
request.initial.image3 }}' accept="media/">
<input type="file" name="image4" value='{{
request.initial.image4 }}' accept="media/">
<input type="file" name="image5" value='{{
request.initial.image5 }}' accept="media/">
<input type="file" name="image6" value='{{
request.initial.image6 }}' accept="media/">
<input type="file" name="image7" value='{{
request.initial.image7 }}' accept="media/">
<input type="file" name="image8" value='{{
request.initial.image8 }}' accept="media/">
<input type="file" name="image9" value='{{
request.initial.image9 }}' accept="media/">
<input type="submit" name="submit" value="Update"
class="btn btn-info" id="post_btn">
<input type=button class="btn btn-default" value="Cancel"
onClick="javascript:history.go(-1);">
</form>
这是我的forms.py
class AddFile(forms.ModelForm):
details = forms.CharField(widget=forms.Textarea)
class Meta:
model = Posts
fields =
('title','city','price','brand','modelyear','condition','mileage',
'details','image','image1','image2','image3','image4','image5',
'image6','image7','image8','image9')