I have a Django form with django-select2 and I would like to submit this one, not with a submit button but with onchange
widget attribute.
I tried to write this :
class ManageDocForm(forms.Form):
def __init__(self, *args, **kwargs):
super(ManageDocForm, self).__init__(*args, **kwargs)
app_list = forms.ModelChoiceField(
queryset=App.objects.all(),
label=_('APP Choice'),
widget=ModelSelect2Widget(
model=App,
search_fields=['code__icontains', 'name__icontains'],
attrs={"onChange": 'actionform.submit();',
'data-placeholder': "Please select an APP"}
)
)
But it doesn't seem to work.
This is my template :
<div class="col-md-12">
<form action="" method="POST">
{% csrf_token %}
<fieldset>
<legend><span class="name">{% trans 'Select an APP' %}</span></legend>
{{ form.app_list }}
</fieldset>
</form>
</div>
And my views.py file :
class ManageDocView(AdminRequiredMixin, View):
""" Render the Admin Manage documents to update year in the filename"""
template_name = 'app/manage_doc_form.html'
form_class = ManageDocForm
success_url = 'app/manage_doc_form.html'
@staticmethod
def get_title():
return 'Change Document Title'
def get(self, request):
form = self.form_class()
context = {
"form": form,
"title": self.get_title()
}
return render(request, self.template_name, context)
def post(self, request):
form = self.form_class()
query_document_updated = None
query_app = None
query_document = None
app_list = request.POST['app_list']
query_app = App.objects.get(id=app_list)
query_document = Document.objects.filter(app=app_list)
context = {
'form': form,
'query_app': query_app,
'query_document': query_document,
'title': self.get_title()
}
return render(request, self.template_name, context)
When I select an APP in my list, it doesn't submit the value and refresh the page?
There is an issue between my widget
and ModelSelect2widget
?