images dont update via form

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 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!

Regarding the files, see File Uploads | Django documentation | Django.

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.

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 :slight_smile:

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

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.

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.

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!