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
In Django, views are Python functions which take a URL request as parameter and return an HTTP response or throw an exception like 404. Each view needs to be mapped to a corresponding URL pattern. This is done via a Python module called URLConf(URL configuration) Let the project name be myProject.
Exact regular expressions, where url order doesn't matter. Notice the regular expressions in listing 2-3 end with the $ character. This is the regular expression symbol for end of line, which means the regular expression urls only match an exact pattern.
Django URL pass parameter to view You can pass a URL parameter from the URL to a view using a path converter. Then “products” will be the URL endpoint. A path converter defines which type of data will a parameter store. You can compare path converters with data types.
Django offers a way to name urls so it's easy to reference them in view methods and templates. The most basic technique to name Django urls is to add the name attribute to url definitions in urls.py .
I was able to make it work using the configuration given below. Check if it will suit your needs.
(?P<name>[\w|\W]+)
If you only want to allow space:
(?P<name>[\w\ ]+)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With