Django:在下拉列表中显示字典的值

2024-10-02 00:42:02 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的模板中,下拉框中填充的是字典,而不是字典的值。我怎样才能使它只显示值?在

这是我的收藏点

class CollectionPoint(models.Model):
    addressID = models.AutoField(primary_key=True)
    collectionPointName = models.CharField(max_length=50, null=False)
    street = models.CharField(max_length=50, null=False)
    streetnumber = models.CharField(max_length=20, null=False)
    city = models.CharField(max_length=50, null=False)
    postalcode = models.CharField(max_length=30, null=True)
    gps_latitude = models.FloatField(blank=True, null=True)
    gps_longitude = models.FloatField(blank=True, null=True)
    country = models.ForeignKey(Country)
    company = models.ForeignKey(Company)

    #only the name is returned to the user
    def __str__(self):
        template = '{collectionPointName}'
        return template.format(collectionPointName=self.collectionPointName)

我想展示所有不同城市的集合点

^{pr2}$

我的观点

@login_required
def rentalselectcity(request):
    # Get the context from the request.
    context = RequestContext(request)

    # A HTTP POST?
    if request.method == 'POST':
        form = RentalSelectCityForm(request.POST)

        # Have we been provided with a valid form?

        return HttpResponseRedirect('/')
    else:
        # If the request was not a POST, display the form to enter details.
        form = RentalSelectCityForm()

    context['path'] = [{'name': 'My rentals', 'url': reverse('rentals-list')}]
    context['path'] += [{'name': 'Select city', 'url': reverse('rental-select-city')}]

    # Bad form (or form details), no form supplied...
    # Render the form with error messages (if any).
    return render_to_response('user/rentalselectcity.html', {'form': form}, context)

还有我的模板

{% block content %}
<div class="box box-default">
    <!--<div class="box-header">
        <h3 class="box-title">Title</h3>
    </div>--><!-- /.box-header -->
    <!-- form start -->
    <form action="{% url 'rental-select-city' %}" method="post" role="form">
        {% csrf_token %}
        <div class="box-body">
            {{ form.non_field_errors }}
            <div class="form-group">
                {{ form.city.errors }}
                <label for="{{ form.city.id_for_label }}">Select a city</label>
                {{ form.city|attr:"class:form-control" }}
            </div>
        </div>
        <!-- /.box-body -->
        <div class="box-footer">
            <button type="submit" class="btn btn-primary">Select city</button>
        </div>
    </form>
</div><!-- /.box -->
{% endblock %}

Tags: thedivformboxtruecitymodelsrequest

热门问题