Add Optional Slash Syntax for Path

215 views
Skip to first unread message

Jason Brill

unread,
Aug 28, 2019, 2:47:39 PM8/28/19
to Django developers (Contributions to Django itself)
As re_path and url syntax utilize regex, true optional trailing slashes are allowed for with the following example:

re_path(r'^about/?$', AboutView.as_view(), name='about')

In this example, a request to both /about and /about/ would yield a 200 response.



Using path would look like this:

path('about/', AboutView.as_view(), name='about')

Assuming we have APPEND_SLASH set to true in our settings, a request to 'about' would yield a redirect with a 300, and a request to 'about/' would yield a 200.


Moreover, using path without the slash would look like this:

path('about', AboutView.as_view(), name='about')

Here we'd obtain a 400 when trying to access 'about/', as intended.


It is impossible to obtain the same behavior when migrating from url/re_path to path. Although it is better design to not allow for two valid endpoints for the same path, I believe we should support the ability to easily maintain the same behavior during migrations.

A solution may be to add a simple parameter for path, so that our route declaration would look something like this:

path('about', AboutView.as_view(), name='about', optional_slash=True)

or

path('about/', AboutView.as_view(), name='about', optional_slash=True)


Another design may be to declare a new global like OPTIONAL_TRAILING_SLASH similar to APPEND_SLASH in our middleware.

Carlton Gibson

unread,
Aug 29, 2019, 1:15:29 AM8/29/19
to django-d...@googlegroups.com
Hi Jason  

I think in this case “just use re_path()” is the way to go. (path() being a convenience, rather than a replacement.) 

For me, complicating the signature of path() for such a corner case wouldn’t be a good trade-off. 

Kind regards,
Carlton. 

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/f74a7074-a66e-4aeb-8b8a-82a845c3f84a%40googlegroups.com.

Markus Holtermann

unread,
Aug 29, 2019, 3:14:42 PM8/29/19
to Django developers
Hi Jason,

Great catch, but I think I agree with Carlton on this one for the reason he mentioned, but even more so for what you already pointed out: "it is better design to not allow for two lvalid endpoints for the same path".

Cheers

Markus

On Thu, Aug 29, 2019, at 7:15 AM, Carlton Gibson wrote:
> Hi Jason
>
> I think in this case “just use re_path()” is the way to go. (path()
> being a convenience, rather than a replacement.)
>
> For me, complicating the signature of path() for such a corner case
> wouldn’t be a good trade-off.
>
> Kind regards,
> Carlton.
>
> On Wed, 28 Aug 2019 at 20:47, Jason Brill <jbri...@gmail.com> wrote:
> > As re_path and url syntax utilize regex, true optional trailing slashes are allowed for with the following example:
> >
> > re_path(r'^about/?$', AboutView.as_view(), name='about')
> >
> > In this example, a request to both /about and /about/ would yield a 200 response.
> >
> >
> >
> > Using *path* would look like this:
> >
> > path('about/', AboutView.as_view(), name='about')
> >
> > Assuming we have *APPEND_SLASH* set to true in our settings, a request to 'about' would yield a redirect with a 300, and a request to 'about/' would yield a 200.
> >
> >
> > Moreover, using *path* without the slash would look like this:
> >
> > path('about', AboutView.as_view(), name='about')
> >
> > Here we'd obtain a 400 when trying to access 'about/', as intended.
> >
> >
> > It is impossible to obtain the same behavior when migrating from url/re_path to path. Although it is better design to not allow for two valid endpoints for the same path, I believe we should support the ability to easily maintain the same behavior during migrations.
> >
> > A solution may be to add a simple parameter for path, so that our route declaration would look something like this:
> >
> > path('about', AboutView.as_view(), name='about', optional_slash=True)
> >
> > *or*
> >
> > path('about/', AboutView.as_view(), name='about', optional_slash=True)
> >
> >
> > Another design may be to declare a new global like OPTIONAL_TRAILING_SLASH similar to APPEND_SLASH in our middleware.
>
> > --
> > You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
> > To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
> > To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/f74a7074-a66e-4aeb-8b8a-82a845c3f84a%40googlegroups.com <https://groups.google.com/d/msgid/django-developers/f74a7074-a66e-4aeb-8b8a-82a845c3f84a%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-develop...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/CAJwKpyR%2Bqytt%3Dw2tPTMtH93EowLuvVgb37RZm2GikCZBx3xjqQ%40mail.gmail.com <https://groups.google.com/d/msgid/django-developers/CAJwKpyR%2Bqytt%3Dw2tPTMtH93EowLuvVgb37RZm2GikCZBx3xjqQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.

Jason Brill

unread,
Aug 29, 2019, 3:51:30 PM8/29/19
to Django developers (Contributions to Django itself)
Hi Carlton,

Thanks so much for the reply. We are temporarily migrating to re_path, however are definitely missing the slug syntax :)
 
Best,
Jason
To unsubscribe from this group and stop receiving emails from it, send an email to django-d...@googlegroups.com.

Bhavesh Kumar

unread,
Aug 29, 2019, 3:51:30 PM8/29/19
to Django developers (Contributions to Django itself)
Hello,


For defining a path/url in django app there are more than one method provided. As per django documentation itself, for complex URL & custom URL developer can either use regex in path or opt with re_path() 
I think the concern you raised might had been discussed in core django team and so they provided more than one way to define it to support wide range of design ideas of any project.


Regards,
Bhavesh Kumar

To unsubscribe from this group and stop receiving emails from it, send an email to django-d...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages