I'd like to build a table with multiple items that have checkboxes, if those checkboxes are checked I want to update just those items via button click.
I already got an update view that does that, but only for one item and only if the save button for this item is pressed (every table item got it's own button). My code looks like 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 so 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 to edit this row item</button>
</th>
<th>
<button type="submit" form="movie{{ forloop.counter }}">Button to save the 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 to edit this row item</button>
</th>
<th>
<button type="submit" form="movie{{ forloop.counter }}">Button to save the changes</button>
</th>
</tr>
</tbody>
<!-- the form for saving exactly one movie -->
<form class="hide" id="movie{{ forloop.counter }}" action="{% url 'myapp:movieDataUpdate' pk=movie.pk %}" method="post">
{% csrf_token %}
</form>
</table>
This is my existing view/urls/form for the Save Button on each row:
urls.py
from django.conf.urls import url
from . 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 someone could help me here, tried to figure it out, but didn't succeeded yet. Maybe someone with a lot more experience can help :)