URL pattern with parameters

I have a url that needs parameters.
Also, since this uses resolve URL, it has two identical URLs.

example

urlpatterns = [
path('path/to/', views.function, name='name'), # 1
path('path/to/?param1=<str:param1>&param2=<str:parame2>', views.function, name='name'), # 2
]
  1. 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?

  2. 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&param2=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'].)

1 Like

URL number 2 is absolutely necessary.
Because it is used in resolve url.

Nope, it is not.
If you need queries in your URL, you are completely in charge of parsing those from requests.
Here is how:

The following entry in urlpatterns is sufficient:
path('path/to/', views.function, name='name'),

Now in the relevant template do:
<a href="{% url 'name' %}?param1={{ param1 }}&param2={{ param2 }}">click me</a>

Finally in your views.py extract the parameters in the query from request.GET

request.GET.get("param1")
request.GET.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.

Just in case a third opinion will change your mind, both @KenWhitesell and @onyeibo are correct, and the second URL pattern is not needed.

1 Like

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

pairs = {
   param1: value1,
   param2: value2,
   param3: value3,
}

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.

1 Like

It’s not an attractive proposition, but it’s a good idea.