0

When I want use argument with specials characters in reverse, I have this error :

Reverse for 'l_s.views.my_pro' with arguments '()' and keyword arguments '{'namep': u'\xe9 \xe9 \xe9 sds ( \xe9zacd '}' not found. 1 pattern(s) tried: ['(?P\w+)$']

My View :

def createPro(request):
    ...
    if form.is_valid() :
        name = form.cleaned_data["name"]
        return redirect(reverse(my_pro, kwargs={'namep': name}))

def my_pro(request,namep):
    pro = Pro.objects.get(name=namep)
    ...

My Template :

...
<form method="POST" action="{% url 'createPro' %}" class="form-signin">
    {% csrf_token %}

    <div class="row">
        <div class="col-md-offset-3 col-md-3">
                {{ form.name|bootstrap }}
        </div>
...

My URL :

url(r'^create-pro$', 'createPro', name='createPro'),
url(r'^(?P<namep>\w+)$','my_pro', name="mypro"),

I have this error when the variable "name" in method "createPro" contains special character. For this example, name = "é é é sds ( ézacd "

YassVegas
  • 179
  • 6
  • 22
  • This doesn't solve your problem - but URI can only contain ASCII characters (see [RFC-2396](http://tools.ietf.org/html/rfc2396.html)) – Burhan Khalid Feb 28 '15 at 13:11
  • @BurhanKhalid: unrelated IRI can contain non-ascii characters (see [rfc 3987](https://tools.ietf.org/html/rfc3987#page-8)). Common application is [IDNA](http://tools.ietf.org/html/rfc5891) – jfs Feb 28 '15 at 13:29
  • You are right, but as far as django in concerned you still need to URL encode it (support for IDN is coming in 1.8). – Burhan Khalid Feb 28 '15 at 13:36

2 Answers2

0

It seems that you want to include unicode character in you url. try this: how to have unicode characters in django url?

Community
  • 1
  • 1
pupil
  • 318
  • 2
  • 16
0

Your test case is name = "é é é sds ( ézacd ", while your regexp for the view is url(r'^(?P<namep>\w+)$'. ' ' and ( are not matched by \w, so that regexp does not match. Try it with "ééésdsézacd", or update your regexp to allow the characters you want to allow in the name.

Code review makes it appear that the Django URL resolver uses the re.UNICODE flag, so I would expect 'é' etc to match \w.

Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83