简体   繁体   中英

Django - Reverse with special character

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 "

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM