python - Django: update only items that are checked -


i'd build table multiple items have checkboxes, if checkboxes checked want update items via button click.

i got update view that, 1 item , if save button item pressed (every table item got it's own button). code looks this:

<table>   <thead>     <tr>       <th colspan="4">         <button type "submit">my submit button</button>       </th>     </tr>     <tr>       <th colspan="2">my title</th>       <th>movie title</th>       <th>movie description</th>       <th>and on</th>     </tr>   </thead>   <tbody>     <tr>       <th>         <input type="checkbox" class="custom-control-input">       </th>       <th>data</th>       <th>data</th>       <th>data</th>       <th>data</th>       <th>         <button>button edit row item</button>       </th>       <th>         <button type="submit" form="movie{{ forloop.counter }}">button save changes</button>       </th>     </tr>     <tr>       <th>         <input type="checkbox" class="custom-control-input">       </th>       <th>data</th>       <th>data</th>       <th>data</th>       <th>data</th>       <th>         <button>button edit row item</button>       </th>       <th>         <button type="submit" form="movie{{ forloop.counter }}">button save changes</button>       </th>     </tr>   </tbody>  <!-- form saving 1 movie -->   <form class="hide" id="movie{{ forloop.counter }}" action="{% url 'myapp:moviedataupdate' pk=movie.pk %}" method="post">  {% csrf_token %}  </form> </table> 

this existing view/urls/form save button on each row:

urls.py

 django.conf.urls import url  . import views   app_name = "myapp"  urlpatterns = [       url(r'^$', views.allmovies.as_view(), name="index"), views.updatemoviedataview.as_view(), name='moviedataupdate'),  ] 

views.py

 class updatemoviedataview(updateview):      model = movie      form_class = updatemoviedataform      success_url = reverse_lazy('myapp:index')       def form_valid(self, form):          self.object.slug = slugify(form.cleaned_data['title'])          return super(updatemoviedataview, self).form_valid(form) 

forms.py

 class updatemoviedataform(forms.modelform):       class meta:            model = movie            fields = ['title', 'date', 'director', 'runtime', 'genre', 'status'] 

i hope me here, tried figure out, didn't succeeded yet. maybe lot more experience can :)

you can add javascript on (jquery easy start).

you first add jquery html page (download here).

then add id checkbox (example below):

<input id="my_checkbox1" type="checkbox" class="custom-control-input"> 

and then, add javascript code (at html) detect checkbox change, , make ajax server.

<script> $("#my_checkbox1").change(function() {     if(this.checked) {         $.post("{% url 'myapp:moviedataupdate' pk=movie.pk %}", {},         function(data, status){             console.log("data: " + data + "\nstatus: " + status);         });     }     # if want, make else }); </script> 

some sources used here:

jquery checkbox checked state changed event

https://www.w3schools.com/jquery/jquery_ajax_get_post.asp


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -