django url pattern for %20

Viewed 14842

In Django what is the url pattern I need to use to handle urlencode characters such as %20

I am using (?P<name>[\w]+) but this only handles alphanumeric characters so % is causing an error

4 Answers

I am using Django 2.2.

It handles the %20 (which means space) in url using Path converter: str

You simply need to use:

<name> or <str:name>

For example, following example loads view "some_view" defined in view.py

#urls.py
from django.urls import path
from . import views
urlpatterns = [
   path("<name>",views.some_view),
   ....
]

The following function renders "some.html" after processing. In this example sending the received name to "some.html".

#view.py
def some_view(request, name):
    # process here
    context = { "name" : name }
    return render(request,"some.html",context)
Related