I want to update the posts i posted,they contain title,city etc fields and 10 image fields(image,image1,image2....etc),whenever i click update, the update form comes with all the text fields filled with previously saved data but image fields are not getting previously submitted images(they're showing empty)..i want them there and let the user change them or delete them etc...can anybody help me?
my images are saving with this method in models.py
def get_image_filename(instance, filename):
title = instance.title
slug = slugify(title)
return "post_images/%s-%s" % (slug, filename)
here's my views for 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})
here's the 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>
it's my 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')