下文描述的表格无效。表达式{{ search_id_form.errors }}不显示任何内容。
Django模板:

        <form method="POST">
            {% csrf_token %}
            {{ search_id_form.idtype.label_tag }}
            {{ search_id_form.idtype }}
            {{ search_id_form.index.label_tag }}
            {{ search_id_form.index }}<br>
            <input type="submit" name="id_search_button" value="Submit">
        </form>


Python类:

class IDSearchForm(forms.Form):

    idtype = forms.ChoiceField(
        choices=[('idx', 'Our Database ID'), ('uprot', 'UniProt'), ('ncbi', 'NCBI')],
        initial='idx',
        widget=forms.RadioSelect,
        label="Which identifier to use:"
        )

    index = forms.CharField(label="Identifier:")


视图:

def search(request):
    if request.method == 'POST':

        # handling other forms ...

        # find a toxin by id
        if 'id_search_button' in request.POST:
            search_id_form = IDSearchForm()
            if search_id_form.is_valid():
                idtype = search_id_form.cleaned_data['idtype']
                index = search_id_form.cleaned_data['index']
                return render(request, 'ctxdb/result_ctx.html', {
                    # here I try to use predefined object to pass to renderer (for debugging)
                    'ctx': get_object_or_404(CTX, idx='21')
                    })

          # handling other forms ...

    # other forms
    search_id_form = IDSearchForm()
    # other forms

    return render(request, 'ctxdb/search.html', {
        # other forms
        'search_id_form': search_id_form,
        # other forms
        })


在视图功能中,我在单个页面上处理四种不同的形式。其他表格可以正常工作。这里有什么问题?

最佳答案

调用.is_valid时,您需要将数据传递给您未使用的search_id_form

更改

search_id_form = IDSearchForm()




search_id_form = IDSearchForm(request.POST)

关于python - 没有显示任何错误的Django表单无效,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32316692/

10-12 03:17