[Answered ]-How to escape '/' in django urls

1👍

From Django models docs:

A slug is a short label for something, containing only letters, numbers, underscores or hyphens.

So the 404 is actually correct, maybe use another field.

0👍

Django path converters match strings in the input URL using regex.
The default path converters are pretty basic – source code.

The slugConverter matches any string that only contains characters, numbers, and dashes, not forward slashes. In string yourtexthere/moretext the largest substring it will match is yourtexthere.

The pathConverter matches a string containing any type of character, so the result can contain a forward slash. It will match all of yourtexthere/moretext. So change your urlpatterns to this:

# Only patterns
urlpatterns = [
    path('', home, name='home'),
    path('<path:slug>/', textview, name='textview'),
]

The pathConverter will match all special characters. If you don’t want this you can create your own custom converter with a regex tailored to your needs. For example, you could simply extend the slugConverter to also match strings with forward slashes.

class SlugPathConverter(StringConverter):
    regex = '[-a-zA-Z0-9_/]+'

Leave a comment