Home > other >  Parse &-character in GET query using Django and Vue
Parse &-character in GET query using Django and Vue

Time:09-28

We are using axios to pass GET request to our django instance, that splits it into search terms and runs the search. This has been working fine until we ran into an edge case. We use urlencode to ensure that strings do not have empty spaces or others

So generalize issue, we have TextField called "name" and we want to search for term "A & B Company". However, issue is that when the request reaches django.

What we expected was that name=A & B Company&field=value would be parsed as name='A & B Company' and field='value'.

Instead, it is parsed as name='A ' 'B Company' and field='value'. The & symbol is incorrectly treated as separator, despite being encoded.

Is there a way to indicate django GET parameter that certain & symbols are part of the value, instead of separators for fields?

CodePudding user response:

You can use the lib urllib

class ModelExample(models.Model):
    name = models.TextField()


# in view...
from urllib.parse import parse_qs

instance = ModelExample(name="name=A & B Company&field=value")
dict_qs = parse_qs(instance.name)

dict_qs contains a dict with decoded querystring

You can find more informations about urllib.parse here: https://docs.python.org/3/library/urllib.parse.html

  • Related