【问题标题】:Django: show value of dictionary in dropdownDjango:在下拉列表中显示字典的值
【发布时间】:2015-07-11 05:27:32
【问题描述】:

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

这是我的收藏点

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)

我想显示所有不同城市的收集点

class RentalSelectCityForm(forms.Form):
    city = forms.ModelChoiceField(queryset=CollectionPoint.objects.order_by().values('city').distinct(),initial=0)

我的看法

@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 %}

【问题讨论】:

    标签: python django templates dictionary dropdownbox


    【解决方案1】:

    您可以将表单字段查询集更改为

    CollectionPoint.objects.order_by().values_list('city', flat=True).distinct() 
    

    参考values_list docs

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 2020-12-23
      • 2022-01-15
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 2014-08-09
      相关资源
      最近更新 更多