Piyush Jiwane
Piyush Jiwane

Reputation: 179

How to write re_path url in django for reading special character?

I am working with Django URL pattern for my project. I am sending a String containing special characters and numbers. While reading a string in python function it won't get the ? in a function.

html

{% for user_name in userSet%}
<a class="dropdown-item" href="/slackNotification/{{user_name}}/{{data.question}}">{{ user_name }}</a>
{% endfor %}

url

path('slackNotification/<str:slack_user_name>/<str:question>',views.slackNotification)

When I get the all details through URL then string whichever I received in the python code it won't contain ? mark even though my input contains Special characters and numbers.

input: What is your name? output: What is your name(question mark not available;)

Question I have tried this:

re_path(r'^slackNotification/<str:slack_user_name>/(?P<question>\w+)',views.slackNotification)

Output The current path didn't match any of the url

I want to know the exact regular expression for my requirement ??

Upvotes: 4

Views: 1949

Answers (2)

Piyush Jiwane
Piyush Jiwane

Reputation: 179

Along with the above solution please refer Url decode UTF-8 in Python link for more information on urllib.parse.quote and urllib.parse.unquote

Upvotes: 0

dirkgroten
dirkgroten

Reputation: 20702

Your first path() is correct as long as each path component in your URL is URL-encoded. ? is a special character in URLs: It denotes the end of the path and anything coming after it is interpreted as query parameters:

http://www.example.com/apples?color=green

has the path http://www.example.com/apples and a query parameter color with value green.

Therefore, if you want to the string "What is your name?" to be included in your path, you need to make sure it's URL-encoded: What%20is%20your%20name%3F where %3F is the URL-encoded ? and %20 is for <space>.

Upvotes: 3

Related Questions