0

It seems there's no way to capture the whole query string in Django, doesn't it? I've only the solutions for capturing individual parameters.

So how can I check whether the query string exists

I want to check whether the query string itself exists (any parameters after "?"), if it does then either replace or add the parameters "param1" to it. How can I do that? For example:

  localhost:8000 -> localhost:8000/?param1=a
  localhost:8000/?param1=1 -> localhost:8000/?param1=bb
  localhost:8000/?param1=1&param2=fdfd -> localhost:8000/?param1=333&param2=fdfd

  localhost:8000/?param2=fdfd -> localhost:8000/?param1=1&param2=fdfd

How can I do that?

1 Answer 1

1

request.GET is is the query string, and it conforms to the dictionary interface. As with all Python containers, an empty dict is False in a boolean context. So you can check if it's empty by just doing if request.GET.

However, in your examples it seems you're always replacing param1 anyway, do there is no need to check it first: just set the value: request.GET['param1'] = 'whatever'.

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

3 Comments

request.GET is immutable by default. You can do request.GET = request.GET.copy() to circumvent this.
How can I convert request.GET to a query string?
Look Here: docs.djangoproject.com/en/1.8/ref/request-response/…. There is a key called QUERY_STRING which should return everything after the ?

Your Answer

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