[Django] #35403: URL path with optional parameter

6 views
Skip to first unread message

Django

unread,
Apr 24, 2024, 4:47:59 PMApr 24
to django-...@googlegroups.com
#35403: URL path with optional parameter
-----------------------------------------------+------------------------
Reporter: Patrick Hintermayer | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 5.0
Severity: Normal | Keywords: url
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------------+------------------------
I sometimes have a class based view which has a GET and a POST method, for
example a ListView which shows some objects with a POST form to create
something out of it.

In the documentation
(https://docs.djangoproject.com/en/5.0/topics/http/urls/) I did not find a
simple way of doing that. Asking GitHub Copilot suggested me adding a
question mark at the end to mark a parameter as optional which does not
work.

**I found myself 2 solutions:**
1. using regex which looks cumbersome just for marking something as
optional:
{{{#!div style="font-size: 80%"
Code highlighting:
{{{#!python
re_path(
r"^activate/(?P<activation_token>[^/]+)?$",
views.UserActivationView.as_view(),
name="user_activation",
)
}}}
}}}

2. using 2 paths: one without a parameter and one with a parameter:
{{{#!div style="font-size: 80%"
Code highlighting:
{{{#!python
path(
"activate/",
views.UserActivationView.as_view(),
name="user_activation",
),
path(
"activate/<str:activation_token>/",
views.UserActivationView.as_view(),
name="user_activation",
)
}}}
}}}

For this ticket, I want to suggest a) improve the documentation with a
simple example how to do that and/or b) can this be simplified in django
by adding for example a question mark at the end like "/<int:some_id?>/"
or "/<int?:some_id>/":
{{{#!div style="font-size: 80%"
Code highlighting:
{{{#!python
path(
"activate/<str:activation_token?>/",
views.UserActivationView.as_view(),
name="user_activation",
)
}}}
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35403>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 25, 2024, 2:46:36 AMApr 25
to django-...@googlegroups.com
#35403: URL path with optional parameter
-------------------------------------+-------------------------------------
Reporter: Patrick Hintermayer | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 5.0
Severity: Normal | Resolution: invalid
Keywords: url | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* resolution: => invalid
* status: new => closed

Comment:

Hi Patrick 👋 I think this ticket is a bit of a mix of a support request,
new feature request, and docs update request.

With support requests (how do I best achieve X with Django), I recommend
asking in the [https://forum.djangoproject.com/c/users/6 forum] because
you will have many more people who can help answer.

I think I would have either gone for solution 2 using a default for
activation_token (a bit like
[https://docs.djangoproject.com/en/5.0/topics/http/urls/#specifying-
defaults-for-view-arguments what is documented here]) or used a query
string like `activate/?token=some-activation-token` and then I can access
token in the [https://docs.djangoproject.com/en/5.0/ref/request-
response/#django.http.QueryDict QueryDict]
([https://docs.djangoproject.com/en/5.0/topics/pagination/#using-
paginator-in-a-view-function example involving pagination]).
I believe we have this functionality already and so something like
`"/<int:some_id?>/"` would not be required.

Before we can discuss a docs update, you need to know what you would have
wanted to see. So I would encourage that you discuss this topic on the
[https://forum.djangoproject.com/c/users/6 forum], agree on an approach,
and then decide if the docs are clear enough or if there is an enhancement
available.
--
Ticket URL: <https://code.djangoproject.com/ticket/35403#comment:1>
Reply all
Reply to author
Forward
0 new messages