0

I am trying to pass a pipe delimited string as a parameter in my url in django, but it fails to route to the respective view.

This is my pipe delimited string:

"main/1|adel|1|1989-09-19|1|1|test alert|2018-03-16-09-26-00|test module|1|1|test encounter"

and this is my url pattern:

from django.conf.urls import url
from django.views.generic import RedirectView
from alerter import views

app_name = 'alerter'

urlpatterns = [

    url(r'^main/(?P<message>[a-zA-Z0-9_]+)/$',
    views.TheView.as_view({'get': 'view'}), name = 'main'),



]
3
  • your regex trying to look for words while you are passing words and special characters... Commented Mar 17, 2018 at 15:29
  • one of the possible solution is replace \w+ with \S+ . or pass the message as url parameter Commented Mar 17, 2018 at 15:32
  • I believe \S+ will capture / in the URL, but yes, the solution is to use an appropriate regex. Commented Mar 17, 2018 at 15:35

1 Answer 1

1

\w doesn't match |:

For Unicode (str) patterns:

Matches Unicode word characters; this includes most characters that can be part of a word in any language, as well as numbers and the underscore. If the ASCII flag is used, only [a-zA-Z0-9_] is matched (but the flag affects the entire regular expression, so in such cases using an explicit [a-zA-Z0-9_] may be a better choice).

Adjust your pattern, e.g. by capturing [a-zA-Z0-9_|]:

url(r'^main/(?P<message>[a-zA-Z0-9_|]+)/$',
    views.TheView.as_view({'get': 'view'}), name='main')
Sign up to request clarification or add additional context in comments.

5 Comments

although i'm convinced it should work, it didn't, the url works if i used non delimited string but even if i used a hyphen as delimiter, it fails.
I frequently use hyphens in URLs. Maybe another rule is matching your URLs. What does "it fails" mean? Do you get a 404, or does a different view load? Please edit your question and add your whole urls.py file.
i get a 404, will add my full file now
@adelelsayed, you don't appear to have included | (or -, for that matter) in your regular expression. Take a close look at mine: I added | after _. (The hyphens in this expression are used to indicate ranges, not as literal hyphens.)
i just forgot to write it here in stakoverflow, i included "- and space" to my regex, it turned out that i forgot about them. url(r'^main/(?P<message>[ a-zA-Z0-9_|-]+)/$', views.TheView.as_view({'get': 'view'}), name = 'main')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.