1

I have the following URL query -

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

however, the spaces are not being decoded and the resulting query that django parses is

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

which returns a null since the string to be found is "SW 6th & Salmon".

Django code

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__'

What is the issue here?

2
  • How sure are you that the problem isn't the unescaped ampersand? Commented Mar 13, 2018 at 23:00
  • You have encoded your parameter wrong. Querystring parameters (as opposed to path elements) should be encoded by replacing spaces by +, not %20. Commented Mar 14, 2018 at 7:49

1 Answer 1

2

The spaces aren't the issue, your ampersand is the problem. Ampersand is the separator between the different parameters, which are name=value pairs.

Parsing your query string results in:

A name of public_location_description with a value of =SW%206th%20
and %20Salmon with no value.

Escape your ampersand by replacing it with %26 (and remove the redundant '=') to get a url of:

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

and try it again

Sign up to request clarification or add additional context in comments.

2 Comments

how do I have Django escape an ampersand automatically when a user enters a '&' in a query parameter?
URLs are not made for human input. That's what HTML forms are for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.