0xE111 / django-mptt-urls

Creating hierarchical URLs in Django associated with django-MPTT models
MIT License
36 stars 5 forks source link

Optional trailing space #12

Closed ValentinCreative closed 4 years ago

ValentinCreative commented 4 years ago

Hi,

On my routes if I omit the trailing space, the view does not return the instance.

I managed to create a fix with a decorator like this :

def check_trailing_slash(view):
    def wrap(request, *args, **kwargs):
        path = kwargs['path']
        if path.endswith('/'):
            return view(request, *args, **kwargs)
        else:
            uri = request.build_absolute_uri()
            return redirect(
                uri.replace(path, f"{path}/"),
                permanent=True,
            )

    return wrap

It works but i found the fix a bit ugly.

Is there something I missed with your library to manage this case ?

0xE111 commented 4 years ago

Hi, I added trailing_slash param to view(...), try smth like this: mptt_urls.view(model='gallery.models.Category', view='gallery.views.category', slug_field='slug', trailing_slash=False) New version of the package (2.0.4) is uploaded to PyPi. Lmk if it works for you.