首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >django模板ModelChoiceField长度

django模板ModelChoiceField长度
EN

Stack Overflow用户
提问于 2012-12-19 19:13:47
回答 1查看 2.1K关注 0票数 2

我使用模板中的length标记来计数下拉列表中的项目数。第一次呈现表单时,长度将很好地显示。当表单被提交时,当长度发生变化时,值不是更新的,而是下拉列表是更新的!为什么?:

在forms.py中:

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

在我的模板中:

代码语言:javascript
复制
{{ 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>

在views.py中:

代码语言:javascript
复制
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))

提前谢谢你,

罗曼

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-19 23:05:03

我想你遇到过第18066期。它被关闭为“需要信息”,但在我看来,它就像一个bug。

作为一项工作,试着

代码语言:javascript
复制
{{ form.lawsToValidate.field.choices.queryset.all|length }}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13959474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档