2

I had a search form in my forms.py:

class search_form(forms.Form):
    text = forms.CharField( widget = 
        forms.TextInput({ "placeholder": "введите слово" }) ) 

and my form header is

<form action='/search_results/' method='get'>

Then I'm getting properties from my base

def search_results(request):
    context = {}
    if request.GET:
        form = search_form(request.GET)
        if form.is_valid():
            print form.cleaned_data['text']
            properties = PropertyText.objects.filter(value__icontains = 
                form.cleaned_data['text'])

If I'm using Latin symbols - I get correct results, but as soon as I'm trying to search for Russian words I fail. For example, I type word для (it's all over my articles) and I get no results. print form.cleaned_data['text'] returns me some strange symbols ÑÑо, and so does print request.GET['text']

encode('utf-8') gives no result, decode('utf-8') throws an exception 'ascii' codec can't encode characters in position 0-5: ordinal not in range(128)

I had # -*- coding: utf-8 -*- in my views.py, forms.py and urls.py and <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> in my template

Community
  • 1
  • 1
birdy90
  • 134
  • 1
  • 2
  • 14
  • 1
    What encoding in your console? Check sql query in django-debug-toolbar. – sneawo Dec 20 '12 at 10:09
  • I'm using putty with UTF-8 set in settings. And query is `SELECT ••• FROM 'technologies_propertytext' WHERE 'technologies_propertytext'.'value' LIKE '%ÑÑо%'` – birdy90 Dec 20 '12 at 15:47
  • Well, i've just dealt with this problem by changing my cleaned_data as folowing: `self.cleaned_data['text'] = self.cleaned_data['text'].encode('iso-8859-1').decode('utf-8')` , but now, when I'm putting my request.GET info back to form (on the next page) with `context['search_form'] = search_form(request.GET or None)` I have the same problem. It returns me `ÑÑо` – birdy90 Dec 22 '12 at 14:12
  • It works for me without encode/decode. Seems that your browser send data in wrong encoding, could you check this? – sneawo Dec 22 '12 at 14:46
  • I found this `HTTP_ACCEPT_CHARSET windows-1251,utf-8;q=0.7,*;q=0.3` in Debug Toolbar in Http headers. In fact I don't know where else can I find this info – birdy90 Dec 22 '12 at 15:44
  • I have this problem in all browsers i have on my computer, and my OS in Windows 7 x64, python 2.6 and django 1.5 – birdy90 Dec 22 '12 at 15:54
  • You use runserver command? – sneawo Dec 22 '12 at 16:19
  • Yes, `... runserver 0.0.0.0:8080` – birdy90 Dec 22 '12 at 16:22
  • I realized that `.encode('iso-8859-1')` was enough for me, it works without `.decode('utf-8')` – birdy90 Dec 22 '12 at 17:25

1 Answers1

1

Check if all of your template files are saved in utf-8 encoding; also, read django docs about unicode data.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Sergey Lyapustin
  • 1,917
  • 17
  • 27
  • I've resaved all my files in UTF-8 with Sublime Text 2, and haven't found any usefull for me in article you poited (maybe I'm so tired and could miss something valuable). Anyway I cheated a bit: I send an encoded string in context straight to my teemplate and form there an input field manually, without using django forms. It works for me fine though it's not absolutely correct. – birdy90 Dec 28 '12 at 09:18