【问题标题】:How to display choicefield select widget on template?如何在模板上显示选择字段选择小部件?
【发布时间】:2012-07-23 06:22:02
【问题描述】:

这是我的看法:

CHOICES = (('10','10'), ('20','20'),('30','30'), ('50','50'))

class Droplist (forms.Form):
    number = forms.ChoiceField(choices = CHOICES)    

    def page_objects(request):
        if request.method == 'POST': # If the form has been submitted...
            form = Droplist(request.POST) # A form bound to the POST data
            if form.is_valid(): # All validation rules pass
                pass #pages = form.cleaned_data['value']
                #return AutoPaginateNode(paginate_by=pages) # Redirect after POST
        else:
            form = Droplist() # An unbound form

        return render_to_response('pagination.html', {'form': form })

这是我的模板:

<form action="/submit/" method="post">{% csrf_token %}
   {{ form }}
   <input type="submit" value="Select">
</form>

我如何呈现我的表单,因为我需要一个带有模板选项的下拉框?我错过了什么?

【问题讨论】:

    标签: django forms drop-down-menu choicefield


    【解决方案1】:

    你需要使用{{ form.as_p }}:

    <form action="/submit/" method="post">{% csrf_token %}
       {{ form.as_p }}
       <input type="submit" value="Select">
    </form>
    

    您也可以使用{{ form.as_table }} 来输出表格行,但您需要提供自己的&lt;table&gt;form.as_ul 来输出列表项:

    <form action="/submit/" method="post">{% csrf_token %}
       <table>
           {{ form.as_table }}
       </table>
       <input type="submit" value="Select">
    </form>
    

    您可以在 Django 的文档中找到有关 Displaying a form using a template 的更多信息

    【讨论】:

    • 它会在我的模板中创建下拉框吗?
    猜你喜欢
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 2012-07-05
    • 2023-03-17
    • 2012-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多