Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

django - How to display choicefield select widget on template?

This is my view:

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 })

This is my template:

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

how can i render my form, because i need to have a dropdown box with choices on template? what i've missed?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use {{ form.as_p }}:

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

You can also use {{ form.as_table }} to output table rows but you'll need to provide your own <table> and form.as_ul to output list items:

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

You can find more information on Displaying a form using a template on Django's documentation


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...