4

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 ??

Piyush Jiwane
  • 179
  • 3
  • 13
  • 1
    A `?` in a url indicates the end of the path and the beginning of the list of query parameters, like in `http://example.com/apples?color=green` the path is `http://example.com/apples`, and `{'color': 'green'}` is the query dict. Only the part before the `?` is considered to be the path. So if you want to include a `?` in a URL you should URL-encode it: `%3F`. Same is true for other special characters, a space for example is `%20` – dirkgroten Feb 17 '20 at 14:44
  • @dirkgroten thank you so much for sharing your knowledge. – Piyush Jiwane Feb 17 '20 at 18:38

2 Answers2

3

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>.

dirkgroten
  • 20,112
  • 2
  • 29
  • 42
0

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

Piyush Jiwane
  • 179
  • 3
  • 13