When resolving URL, the question mark(?) is URL encoded as %3F. (/path/to/%3F{params})
In this case, it is recognized as a different URL than the URL I intended.
Is there a way to get this in “/path/to/?{params}” format?
I want to use only URL number 2.
However, if you comment out URL 1, Django determines that there is no matching URL even if you access the correct URL.
Is there any way to fix this?
Everything including and after the question mark - ? is not part of the url and does not factor into the url resolution process. You don’t need to have that second version. (It’s not going to do anything for you.)
So it’s not URL 1 that you want to comment out, it’s URL 2.
Submitting a request like http://www.yoursite.com/path/to/?param1=abc¶m2=def is going to resolve to the first url you listed. Those parameters can then be retrieved in that view from request.GET. (In this case, it would be request.GET['param1'] and request.GET['param2'].)
I know that I can get parameters from request.GET.
In case someone has the same problem as me, I would like to know if there is a better way to write URL patterns.
(Probably the method I am using now is the best.)
I said it was clearly needed for the resolve URL.
This method is cumbersome because I have to check the format myself every time I create a link to make sure it is formatted correctly.
Also, when the URL pattern changes, you have to find each URL written in that way one by one, which is very inconvenient.
Perhaps @white-seolpyo wants the query part of the URL to be dynamic. Therefore, the parameter-value pair varies. He could prepare a dictionary of pairs in his view
Then pass pairs into the context dictionary. This allows pairs to vary … it can shrink or expand at runtime depending on the conditions that @white-seolpyo specifies. This approach requires a tweak in the template:
<a href="{% url 'name' %}?{% for param, value in pairs %}{{ param }}={{ value }}&{% endfor %}">
click me
</a>
Now @white-seolpyo can have fun changing pairs, and the URL will still resolve accordingly. There is only one caveat.