update model form with json infomration

hello everyone,
i have mad a custom admin page using django forms.
I have a form that I want to set values dynamicly using an exiting model that come via ajax request.

the html:

{% csrf_token %}
שם הקורס {{course_form.name}}
                    <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}}!-->
                        {{course_form.autor}}

                        <span class="input-name">מוכן להצגה</span>
                        <!--{{course_form.autor}}!-->
                        {{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">
                        <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>

            </form>


i recive the information via ajax and i want to update the form (set the name and stuff), is there a django/ajax function to do so or do i need to make it be hand?

For clarity, are you looking to update the form, or are you looking to update the model based upon the data submitted via AJAX?

to be honest for both of the.
once i click a button, i request information about a spesefic model, retrive it with ajax and update the form with the model context. than the user needs to change it, and once he submits the form via ajax i need to reupdate it in the db.

thanks for the quick respone.

Ok, so breaking this down into the sequence of events:

  • User requests a page
  • User clicks a button on the page
  • JavaScript on the page submits an AJAX request
  • Server returns a JSON Response
  • JavaScript updates input fields (and possibly other data) on the page from that response
  • User is able to edit the form.
  • User clicks a different button to submit the form.
  • Server processes the submission.

Is this a correct summary?

I have done everything expect

  • JavaScript updates input fields (and possibly other data) on the page from that response
    is there a “cool django way” to do it, or should i acsses each input one by one via js and update?

Django isn’t involved at all on the browser side with the dynamic updating of a page. That’s a process that must be done with JavaScript.

About the best that Django can do is to create the JSON object with a structure that facilitates the JavaScript doing the updating in the client.

i see, thanks so much for the help! you have helped me since i started and i really appreciate it!