user266003
user266003

Reputation:

Add or replace a parameter in the query string

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?

Upvotes: 0

Views: 430

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599788

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

Upvotes: 1

Related Questions