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
291 views
in Technique[技术] by (71.8m points)

Display a form field i.e., a choice field of form in the template django

I've have forms.py file in that i have a choice field which i've to display it in the template.html

forms.py

 Choices = [('Yelp',)]
 class UtilitiesForm(forms.Form):
     api_select = forms.MultipleChoiceField(widget=forms.Select(), 
                  choices=Choices)
     text_url = forms.CharField()

template.html

{% block body_content %}

  <form action="/utilities/fetch-data/" method="post" id="utitliy__form">
     <div class="form-row">
         <label>Select an API</label>
           {{ form.api_select }}
     </div>
  </form>
 {% endblock body_content %}

i'm getting Value error can you guys help me how to write the choice field in template.html

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 not to do anything with choice field in forms.py you can directly use it in your template. template

<form action="" method="post">
        {% csrf_token %}
        <label for="order_no">Order No.:</label>
        <input type="text" class='form-control' value="{{Order}}" name="order_no" readonly><br>
        <label for="isbn">ISBN No.:</label>
        <input type="text" class='form-control' value="{{ISBN}}" name="isbn" readonly><br>
        <label for="rate">Rate:</label>{{forms.rate}}(Rate us 10 is the Highest and 1 is the lowest)<br><br>
        <label for="comment">Comments</label>{{forms.comment}}<br><br>
        <input type="submit" class="btn btn-success">
</form>

Models.py

RATING=( (1,1), (2,2), (3,3), (4,4), (5,5), (6,6), (7,7), (8,8), (9,9), (10,10) )
class returnbook(models.Model):
    order_no=models.IntegerField(blank=True,null=True)
    isbn=models.CharField(max_length=15)
    rate=models.IntegerField(choices=RATING,default='1')
    comment=models.TextField(max_length=20000,blank=True)
    user=models.CharField(max_length=50) 

class Meta:
    unique_together = ('order_no', 'isbn')
    def __unicode__(self):
        return unicode(self.rate)

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

...