Linux
Unicode Character Support in Django URLs
26th Tem 2009 | Sınıfı: Linux 0

If you were to use an unicode character in your URL that you request, you would probably face a URL matching error. Because urls.py file supports ASCII URLs by default. If you specify patterns like [a-z] or \w as regular expression content, the unicode characters like ‘ö’ won’t get matched with this regular expression. So, you have to first specify that matching will be unicode and secondly write all the letters individually in the matching range. Below is a partial urls.py file:

urlpatterns = patterns('search.pathsearch.views',
     (u'^$', 'index'),
     (u'^(?P<version>[-0-9A-Za-z]+)/$', 'index'),
     (u'^(?P<version>[-0-9A-Za-z]+)/package/(?P<package_name>[-_.0-9A-Za-zıİğĞüÜşŞöÖçÇ ]+)/$', 'list_package_contents'),
)

Instead of this hard coded solution, you can accept all the characters and then filtrate the ones you want to really accept as you can see here. But you have to be careful and filtrate carefully when using this method. Other warning will be that older versions of Python and Django can cause problems in matching unicode characters.

Etiketler: , ,
Leave a Reply

IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)

What is 12 + 9 ?
Please leave these two fields as-is: