# images dont update via form

**URL:** <https://forum.djangoproject.com/t/images-dont-update-via-form/12597>\
**Category:** Forms & APIs\
**Created:** [March 14, 2022, 2:28pm UTC](https://forum.djangoproject.com/t/images-dont-update-via-form/12597 "2022-03-14T14:28:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![eyalyakir159](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/eyalyakir159/32/5532_2.png) [@eyalyakir159](https://forum.djangoproject.com/u/eyalyakir159)\
**Post date:** [March 14, 2022, 2:28pm UTC](https://forum.djangoproject.com/t/images-dont-update-via-form/12597/1 "2022-03-14T14:28:26Z")

</div>

hello,  
I have made a custom adminpannel were the user need to update a picture to a spasefic model by using django forms like so -

model -  
class course(models.Model):

```
name = models.CharField(max_length=50)

description = models.CharField(max_length=500)

title_description = models.CharField(max_length=250,default="")

course_slug = models.CharField(max_length=50,default="")

price = models.FloatField()

blocked_by = models.ManyToManyField('course',related_name="blocking",blank=True,default=None)

image = models.ImageField(upload_to="uploads",blank=True,default=None)

course_sections = models.ManyToManyField(course_section,related_name="course",blank=True)

autor = models.ForeignKey(NewUser, related_name='writen_courses',

                          on_delete=models.DO_NOTHING, blank=True, null=True)

degree = models.ManyToManyField(degree,related_name="courses", blank=True)

display = models.BooleanField(default=True)

```

model form -  
class courseForm(ModelForm):

```
class Meta:

    model = course

    exclude=('one_to_one_course','course_sections')

    widgets = {

        "description": forms.Textarea()

    }

```

html form

```
                {% csrf_token %}

                <div class="input_double">

                    <div class="input-con">

                        <span class="input-name">שם הקורס</span>

                        {{course_form.name}}

                    </div>

                    <div class="input-con">

                        <span class="input-name">מספר קורס</span>

                        {{course_form.course_slug}}

                    </div>  

                </div>

                <div class="input_single">

                    <div class="input-con">

                        <span class="input-name">תיאור הקורס</span>

                        {{course_form.description}}

                    </div>

                </div>

                <div class="input_double">

                    <div class="input-con">

                        <span class="input-name">משפט מפתח</span>

                        {{course_form.title_description}}

                    </div>

                    <div class="input-con">

                        <span class="input-name">מחיר</span>

                        {{course_form.price}}

                    </div>  

                </div>

                <div class="input_double extra_margin">

                                           

                    <div class="input-con autor_selected">

                        <span class="input-name">שם היוצר</span>

                        {{course_form.autor}}

                        <span class="input-name">תואר</span>

                        {{course_form.degree}}

                        <span class="input-name">מוכן להצגה</span>

                        {{course_form.display}}

                    </div>  

                    <div class="input-con selected_div">

                        <span class="input-name">קורסים חוסמים</span>

                        {{course_form.blocked_by}}

                    </div>

                </div>

                <div class="full_width">

                    <div class="image_container">

                        <img style="background-image:url({% static 'main_website\images\graduation.png' %})" id="display_image" alt="">

                    </div>

                    <div class="input-con center_on_image">

                        {{course_form.image}}

                        <label for="upload"><i class="fas fa-upload"></i></label>

                    </div>

                   

                </div>

                <div class="full_width_button">

                    <button class="cta" method="submit" name="sumbit_course">

                        <span>עדכן פרטים</span>

                        <svg width="15px" height="10px" viewBox="0 0 13 10">

                            <path d="M11,5 L1,5"></path>

                            <polyline points="4 1 0 5 4 9"></polyline>

                        </svg>

                    </button>

                </div>

                {{course_form.errors}}

            </form>

```

view handling  
course\_form=courseForm(request.POST)

```
        if course_form.is_valid():

            if request.POST["course_id"]!="-1": # not a new course -> need to update exitsting one

                updated_course = course.objects.get(id=request.POST["course_id"])

               

                course_form=courseForm(request.POST,instance=updated_course)

                new_course = course_form.save()

            else: # saving new model

                new_course = course_form.save()

                return JsonResponse({"name":new_course.name,"id":new_course.id})

        else:

            print(course_form.errors)

```

now everything gets updated and the data is stored but when i try to acsses the image that was updated i get an erorr not found ([http://127.0.0.1:8000/uploads/graduation.png](http://127.0.0.1:8000/uploads/graduation.png) 404 (Not Found)).  
but, if i update it via django admin it is working.  
its like the form dosent save the correct url…

and another question related to js and html.  
i have an date input, and i want to render it to Date class when it is changed,  
so i added an event lister  
start\_date\_search.addEventListener(‘input’,filterList\_payments);  
and the function dose that  
const start\_date\_filter = Date(start\_date\_search.value)  
but insted of having the correct date, it gives me a random year but the day and month are correct.  
any ideas?

thanks alot!

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 14, 2022, 11:02pm UTC](https://forum.djangoproject.com/t/images-dont-update-via-form/12597/2 "2022-03-14T23:02:08Z")

</div>

Regarding the files, see [File Uploads | Django documentation | Django](https://docs.djangoproject.com/en/4.0/topics/http/file-uploads/#basic-file-uploads).

> [@eyalyakir159](#):
>
> and another question related to js and html.  
> i have an date input, and i want to render it to Date class when it is changed,  
> so i added an event lister  
> start\_date\_search.addEventListener(‘input’,filterList\_payments);  
> and the function dose that  
> const start\_date\_filter = Date(start\_date\_search.value)

Can you clarify or explain this better? I’m not following what you’re trying to ask here. If you have an input field in a form that is a reference to a Date field in a model, it will be stored as a Date field. So I’m not understanding what or why you think additional processing is necessary.

---

<div class="post-metadata">

**Author:** ![eyalyakir159](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/eyalyakir159/32/5532_2.png) [@eyalyakir159](https://forum.djangoproject.com/u/eyalyakir159)\
**Post date:** [March 14, 2022, 11:20pm UTC](https://forum.djangoproject.com/t/images-dont-update-via-form/12597/3 "2022-03-14T23:20:38Z")

</div>

i mixed up my questions  
first question :  
bascily i have image field in a model, and when I add the model form with django form and do form.save() all the data gets updated, the text, the dates etc but the image dosent.

about the second option i have figued it out, thanks anyway 🙂

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 14, 2022, 11:22pm UTC](https://forum.djangoproject.com/t/images-dont-update-via-form/12597/4 "2022-03-14T23:22:17Z")

</div>

Yes, that’s why I referred you to the docs on file uploads that I highlighted in my previous reply.

---

<div class="post-metadata">

**Author:** ![eyalyakir159](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/eyalyakir159/32/5532_2.png) [@eyalyakir159](https://forum.djangoproject.com/u/eyalyakir159)\
**Post date:** [March 15, 2022, 12:29am UTC](https://forum.djangoproject.com/t/images-dont-update-via-form/12597/5 "2022-03-15T00:29:07Z")

</div>

hey,  
doing what the documentaion says I still dosent work,  
am I missing something?

view  
if request.method == “POST”: #if its user submition

```
    if "course_id" in request.POST: #if its course sumbtion

        course_form=courseForm(request.POST,request.FILES)

        if course_form.is_valid():

            if request.POST["course_id"]!="-1": # not a new course -> need to update exitsting one

               

                updated_course = course.objects.get(id=request.POST["course_id"])

               

                course_form=courseForm(request.POST,request.FILES,instance=updated_course)

                new_course = course_form.save()

            else: # saving new model

                new_course = course_form.save()

                return JsonResponse({"name":new_course.name,"id":new_course.id})

        else:

            print(course_form.errors)

```

```
                {% csrf_token %}

                <div class="input_double">

                    <div class="input-con">

                        <span class="input-name">שם הקורס</span>

                        {{course_form.name}}

                    </div>

                    <div class="input-con">

                        <span class="input-name">מספר קורס</span>

                        {{course_form.course_slug}}

                    </div>  

                </div>

                <div class="input_single">

                    <div class="input-con">

                        <span class="input-name">תיאור הקורס</span>

                        {{course_form.description}}

                    </div>

                </div>

                <div class="input_double">

                    <div class="input-con">

                        <span class="input-name">משפט מפתח</span>

                        {{course_form.title_description}}

                    </div>

                    <div class="input-con">

                        <span class="input-name">מחיר</span>

                        {{course_form.price}}

                    </div>  

                </div>

                <div class="input_double extra_margin">

                                           

                    <div class="input-con autor_selected">

                        <span class="input-name">שם היוצר</span>

                        {{course_form.autor}}

                        <span class="input-name">תואר</span>

                        {{course_form.degree}}

                        <span class="input-name">מוכן להצגה</span>

                        {{course_form.display}}

                    </div>  

                    <div class="input-con selected_div">

                        <span class="input-name">קורסים חוסמים</span>

                        {{course_form.blocked_by}}

                    </div>

                </div>

                <div class="full_width">

                    <div class="image_container">

                        <img id="display_image" alt="">

                    </div>

                    <div class="input-con center_on_image">

                        {{course_form.image}}

                        <label for="upload"><i class="fas fa-upload"></i></label>

                    </div>

                   

                </div>

                <div class="full_width_button">

                    <button class="cta" method="submit" name="sumbit_course">

                        <span>עדכן פרטים</span>

                        <svg width="15px" height="10px" viewBox="0 0 13 10">

                            <path d="M11,5 L1,5"></path>

                            <polyline points="4 1 0 5 4 9"></polyline>

                        </svg>

                    </button>

                </div>

                {{course_form.errors}}

            </form>

```

html  
for some reason it dosent show but my form has enctype=“multipart/form-data” and method post

thanls.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 15, 2022, 12:49am UTC](https://forum.djangoproject.com/t/images-dont-update-via-form/12597/6 "2022-03-15T00:49:28Z")

</div>

In a situation such as this, my recommendation is to start small.

I’m seeing too much logic in the view to make it obvious what might be going wrong.

If you haven’t worked with file uploads before, then build yourself a test model, with a simple form and view. Get rid of everything that isn’t absolutely needed and get that working first. Once you have that working, then you have a pattern to work from as you add functionality.

---

<div class="post-metadata">

**Author:** ![eyalyakir159](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/eyalyakir159/32/5532_2.png) [@eyalyakir159](https://forum.djangoproject.com/u/eyalyakir159)\
**Post date:** [March 15, 2022, 2:05am UTC](https://forum.djangoproject.com/t/images-dont-update-via-form/12597/7 "2022-03-15T02:05:29Z")

</div>

listened to your advice and it worked, after 2 hours. btw the problem was that ajax requests are delt diffrently from normal post request. (you cant use form.starlizer). such a pain in the ass.

thanks so much!
