0

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')
Foki
  • 17
  • 1
  • 6
  • 1
    Please reformat your code and question so we can help you better. – Noxeus Apr 06 '18 at 11:11
  • In all the other fields you use `form.initial.whatever`, but in the images you use `request.initial`. I don't usually do this manually, so I'm not sure, but I'm guessing that if you use `form` it will work as you intend. – Paulo Almeida Apr 06 '18 at 11:20
  • Unrelated but your view is not doing things the right way (it should redirect on a successful post and render the invalid form if validation failed), and your template code is wrong too (it doesn't display validation errors). – bruno desthuilliers Apr 06 '18 at 11:24
  • And the real problem has nothing to do with django actually, cf the duplicate question. – bruno desthuilliers Apr 06 '18 at 11:27
  • @PauloAlmeida whats should i do to replace form.initial.whatever so that i could get their values?plus i know my views is redirecting it to the same page because i'm just testing here,i'll change the redirect page after every thing's working fine – Foki Apr 06 '18 at 11:37
  • @Foki It's the other way around. `form.initial.whatever` is correct, you should also use `form.initial.image1`, etc, instead of `request.initial.image`. – Paulo Almeida Apr 06 '18 at 11:40
  • @PauloAlmeida i've tried this before but it's not working at all,i can't get the images i previously uploaded while editing my post,its showing nothing but blank image browse field,it should show the previously uploaded images with the post – Foki Apr 06 '18 at 11:43
  • @Foki Ok, that's because of what's in the duplicate, I didn't think of that. You should do it like the Django admin: you add a link to the current file and the actual `input` HTML element is empty. – Paulo Almeida Apr 06 '18 at 11:46
  • @PauloAlmeida ok but how to do it like django admin?any link or example for doing this? – Foki Apr 06 '18 at 11:48
  • @Foki Well, the easiest would be to use the browser developer tools to see what's in the admin page, but it's really just a link: `"File name"`. Of course, the path will be dynamic, based on your media URL. I marked this question for `reopen`, because I thought it was a useful distinction (the duplicate's accepted answer is right that you can't do it, but doesn't suggest alternatives), but now I saw there is at least one answer there that suggests what I just said. – Paulo Almeida Apr 06 '18 at 11:54
  • @PauloAlmeida i tried other answers but couldn't find any help for my issue – Foki Apr 06 '18 at 11:58
  • @Foki My previous comment solves your issue: just create a link pointing to the file. And that's what [the answer I was referring to](https://stackoverflow.com/a/1696905/1081569) suggests. If you can't figure it out you can open a new question with your attempt and why it fails. – Paulo Almeida Apr 06 '18 at 12:01
  • @PauloAlmeida i understand what you're saying but i don't understand where to put that "File name" tag in my html,,should i add this to the every input files field? – Foki Apr 06 '18 at 12:06
  • @PauloAlmeida 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) – Foki Apr 06 '18 at 12:07
  • @Foki Yes, you should have one link for each input file field. The Django admin has that link directly above the `input` element for the file, but you can style it however you want it. – Paulo Almeida Apr 06 '18 at 12:09
  • @PauloAlmeida i have edited and showed my method for saving images on the top in my post – Foki Apr 06 '18 at 12:09
  • @PauloAlmeida thanks, i'm trying – Foki Apr 06 '18 at 12:13
  • @PauloAlmeida but it's showing "no file chosen" with image browse icon..how to get rid of that if there's an image? – Foki Apr 06 '18 at 12:15
  • @PauloAlmeida and how will it include the original image if user lete the previous image attached and update the post?hope you can help – Foki Apr 06 '18 at 12:16
  • @Foki Like I said, if you can't do it, please open a new question, this isn't something to do over comments and it's outside the scope of the original question. – Paulo Almeida Apr 06 '18 at 12:17
  • @PauloAlmeida ok thanks for your help ;) – Foki Apr 06 '18 at 12:38

0 Answers0