I’ve tried to create a form, to view and select multiple choices, to delete them.
I’ve tried to make this with ModelForm, but was unable to get a good way to do it.
With forms.Forms I did this :
class DeleteQuotesForm(forms.Form):
Choices = Quote.objects.values('id')
picked = forms.MultipleChoiceField(choices=Choices, widget=forms.CheckboxSelectMultiple)
I chosed objects.values(‘id’) because using objects.all() I got an error saying the values where unpackable. Maybe because it give a QuerySet, and the forms do not manage it in the template ?
Now, I get a ValueError saying “not enough values to unpack (expected 2, got 1)” … it seems to be linked with the “return” line of the view :
def suppression_quote(request):
if request.method == 'POST':
form = DeleteQuotesForm(request.POST)
Quote.objects.filter(form.picked).delete()
else:
form = DeleteQuotesForm
return render(request, 'Citatio/suppression_quote.html', {'form':form})
Also, no sure of the method I use to get select value to delete them (Quote.objects.filter(form.picked).delete()
Here is the template suppression_quote.html :
{% block content %}
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Enregistrer</button>
</form>
{% endblock %}
Thanks for your lights