Hi,
Using the following code, I get an error 405 in the inspector. Any idea what could be wrong here? I’ve tried different things but with no success so far.
Thanks in advance for your suggestions.
views.py
def adherent_view(request):
if request.method == 'POST':
form = AdherentForm(request.POST)
if form.is_valid():
qdem01_genre = form.cleaned_data['qdem01_genre']
qdem01rx_genre_precis = form.cleaned_data['qdem01rx_genre_precis']
qdem02_naissance = form.cleaned_data['qdem02_naissance']
qdem03_diplome = form.cleaned_data['qdem03_diplome']
qdem04_csp = form.cleaned_data['qdem04_csp']
obj = Questionnaire(
qdem01_genre = qdem01_genre,
qdem01rx_genre_precis = qdem01rx_genre_precis,
qdem02_naissance = qdem02_naissance,
qdem03_diplome = qdem03_diplome,
qdem04_csp = qdem04_csp
)
obj.save()
form2 = AdherentForm(request.POST, instance=obj)
form2.save(commit=False)
form2.save_m2m()
return redirect('')
else:
form = AdherentForm()
context = {'form':form}
return render(request, 'form1/adherent.html',context)
class MerciView(TemplateView):
template_name = 'form1/merci.html'
urls.py
urlpatterns = [
path('adherent/', views.adherent_view, name='adherent'),
path('merci/', views.MerciView.as_view(), name='merci'),
]
forms.py
class AdherentForm(forms.ModelForm):
qdem05_statut = forms.ModelMultipleChoiceField(
queryset=Statut.objects.all(),
widget=forms.CheckboxSelectMultiple,
label=('label')
)
class Meta:
model = Questionnaire
# fields = ['qdem01_genre', 'qdem01rx_genre_precis', 'qdem02_naissance', 'qdem03_diplome', 'qdem04_csp', 'qdem05_statut', 'qdem07_revenus', 'qdem08_patrimoine', 'qdem09_territoire', 'qdem10_departement', 'qdem10b_pays', 'qcon02_consent', 'qdem11_discrim1', 'qdem12_discrim2', 'qtraj01_avant_adh', 'qtraj01_avant_sym', 'qtraj02_adhes_adh', 'qtraj02_adhes_sym', 'qtraj03_deseng_mil', 'qtraj04_deseng_adh', 'qtraj05_deseng_cause', 'qtraj06_primo_eng', 'qtraj07_integ_sent', 'qtraj08a_mandat']
exclude = ['created_at','edited_at']
# widgets = {
# 'qdem05_statut': forms.CheckboxSelectMultiple
# }
def __init__(self, *args, **kwargs):
kwargs.setdefault('label_suffix', '')
super(AdherentForm, self).__init__(*args, **kwargs)
adherent.html
{% extends 'form1/base.html' %}
{% block content %}
{% load static %}
<form action="{% url 'merci' %}" method="post" id="form" name="form">
{% csrf_token %}
<div id="form_all">
{{ form.non_field_errors }}
<div name="genre" class="questionWrapper" id="genre">
<p>{{ form.qdem01_genre.errors }}</p>
<p>{{ form.qdem01_genre.label_tag }}</p>
<p>{{ form.qdem01_genre }}</p>
</div>
<div name="genre_preciser" class="questionWrapper" id="genre_preciser">
<p>{{ form.qdem01rx_genre_precis.errors }}</p>
<p>{{ form.qdem01rx_genre_precis }}</p>
</div>
<div name="naissance" class="questionWrapper" id="naissance">
<p>{{ form.qdem02_naissance.errors }}</p>
<p>{{ form.qdem02_naissance.label_tag }}</p>
<p>{{ form.qdem02_naissance }}</p>
</div>
<div name="diplome" class="questionWrapper" id="diplome">
<p>{{ form.qdem03_diplome.errors }}</p>
<p>{{ form.qdem03_diplome.label_tag }}</p>
<p>{{ form.qdem03_diplome }}</p>
</div>
<div name="csp" class="questionWrapper" id="csp">
<p>{{ form.qdem04_csp.errors }}</p>
<p>{{ form.qdem04_csp.label_tag }}</p>
<p>{{ form.qdem04_csp }}</p>
</div>
<div name="statut" class="questionWrapper" id="statut">
<p>{{ form.qdem05_statut.errors }}</p>
<p>{{ form.qdem05_statut.label_tag }}</p>
<p>{{ form.qdem05_statut }}</p>
</div>
</div>
<input type="submit" value="Envoyer">
</form>
{% endblock %}
{% block script %}
<script src="{% static 'genre.js' %}"></script>
{% endblock %}
and finally merci.html
{% extends 'form1/base.html' %}
{% block content %}
Merci pour votre participation !
{% endblock %}