url查询中的空格未在django中解码

时间:2018-03-13 22:58:09

标签: python django django-rest-framework

我有以下网址查询 -

http://localhost:8000/api/passenger-census/?public_location_description==SW%206th%20&%20Salmon

但是,空格未被解码,并且django解析的结果查询是

GET /api/passenger-census/?public_location_description=SW%206th%20&%20Salmon

返回null,因为要找到的字符串是" SW 6th&鲑鱼&#34 ;.

Django代码

views.py -

class PassengerCensusViewSet(viewsets.ModelViewSet):

    queryset = PassengerCensus.objects.all()
    serializer_class = PassengerCensusSerializer
    filter_backends = (SearchFilter,DjangoFilterBackend,OrderingFilter,)
    search_fields = ('route_number', 'direction','service_key','stop_seq',
                     'location_id','public_location_description',)
    filter_fields = ('summary_begin_date','route_number','direction','service_key','stop_seq','location_id',
                    'public_location_description','ons','offs','x_coord','y_coord','geom_2913','geom_4326',)
    ordering_fields = '__all__'

serializer.py

class PassengerCensusSerializer(serializers.ModelSerializer):

    class Meta:
        model = PassengerCensus
        fields = '__all__'

这里有什么问题?

1 个答案:

答案 0 :(得分:2)

空间不是问题,你的&符是问题所在。 Ampersand是不同参数之间的分隔符,它们是名称=值对。

解析查询字符串会导致:

名称public_location_description,其值为=SW%206th%20
%20Salmon没有任何价值。

将{< {1}}替换为&符号(并删除多余的' =')以获取以下网址:

http://localhost:8000/api/passenger-census/?public_location_description=SW%206th%20%26%20Salmon

再试一次