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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018f11dd6639-19e8a5d2-0c87-4434-92ae-538b89594861-000000%40eu-central-1.amazonses.com.

Reply via email to