Question

I use the length tag in my template to count the number of items in my drop down list. The length is well displayed the first time the form is rendered. When the form is submitted and when the length has changed, the value is not updated but the drop down list is updated! Why? :(

In forms.py:

lawsToValidate=forms.ModelChoiceField(queryset=LawsIdsModel.objects.filter(validated=0), empty_label="Select a law to validate", widget=forms.Select(attrs={'onchange': 'this.form.submit();'}))

In my template:

{{ form.lawsToValidate.field.choices|length }}
    <form id="lawsIdsForm" action="{% url lawsValidation.views.lawsView %}" method="post">{% csrf_token %}
        {{ form.non_field_errors }}

        {{ form.lawsToValidate.field.choices|length }} laws to validate!

        <div id="lawsToValidateChoice" class="fieldWrapper">
            {{ form.lawsToValidate.errors }}
            {{ form.lawsToValidate }}
        </div>
    </form>

In views.py:

def lawsView(request):
    responseDic={}
    state="display"
    if request.method == 'POST':
        lawToValidate=request.POST.getlist('lawsToValidate')[0]
        #if a law is selected
        if lawToValidate!="":
            law=LawsIdsModel.objects.get(id=lawToValidate)
            #saves the law
            if 'lawsValidationSaveButton' in request.POST:
                law.validated=True
                form = LawsIdsForm(request.POST, instance=law)
                if form.is_valid():
                    form.save()
                    del form
                    state="saved"
                    responseDic['success']="The law releveAnnee=" + str(law.releveAnnee) + ", releveMois=" + str(law.releveMois) + ", noOrdre=" + str(law.noOrdre) + " has been validated!"
                else:
                    state="ongoing"
            #displays the retrieved information of the law to validate (selection of a law in the drop down list)
            if state!="saved":
                #a law has been selected in the drop down list -> the related information are displayed
                if state=="display":
                    form = LawsIdsForm(instance=law, initial={'lawsToValidate': lawToValidate, 'releveAnnee': law.releveAnnee, 'releveMois': law.releveMois, 'noOrdre': law.noOrdre})
                #an error occured while validating the law -> display of these errors
                elif state=="ongoing":
                    form = LawsIdsForm(request.POST, instance=law, initial={'lawsToValidate': lawToValidate, 'releveAnnee': law.releveAnnee, 'releveMois': law.releveMois, 'noOrdre': law.noOrdre})
                responseDic['form']=form
                responseDic['law']=law
    #~ #if form has not been created yet -> unbound form
    if 'form' not in locals():
        responseDic['form'] = LawsIdsForm()
    return render_to_response('lawsValidation/index.html', responseDic, context_instance=RequestContext(request))

Thank you in advance,

Romain

Was it helpful?

Solution

I think you've come across issue 18066. It's been closed as 'needs info', but it looks like a bug to me.

As a work around, try

{{ form.lawsToValidate.field.choices.queryset.all|length }}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top