Home > Software design >  Django : meaning of {% url 'zzz' %}?{{ yyy }}?
Django : meaning of {% url 'zzz' %}?{{ yyy }}?

Time:12-08

I came across a code in a HTML file and I did not understand how it works.

{% url 'calendar' %}?{{ prev_month }}

Especially, what does the "?" mean? And how is the code interpreted?

It is used in a button to switch between month but I cant figure out how it works.

I tried to print different places in the code to see what is executed with but I did not find anything interesting.

Thanks

CodePudding user response:

Especially, what does the "?" mean?

This is for the query string [wiki]. Indeed, if for example prev_month contains foo, it will make a query that looks like:

/some/path?foo

The querystring is a key-value dictionary where the same key can map to multiple values. You can access this with request.GET [django-doc]. Usually it is thus provided, for example as:

/some/path?foo=bar

If you use the item as value, you should work with the |urlencode template filter [django-doc]:

{% url 'calendar' %}?month={{ prev_month|urlencode }}

If the prev_month then for example itself contains a question mark (?), ampersand (&), etc. that will be escaped properly.

  • Related