#3412: Search unicode string in DB
-------------------------------------------+--------------------------------
Reporter:  Sébastien ( [EMAIL PROTECTED] )  |       Owner:  adrian         
  Status:  new                             |   Component:  django.newforms
 Version:  SVN                             |    Keywords:                 
   Stage:  Unreviewed                      |   Has_patch:  0              
-------------------------------------------+--------------------------------
 I try to search records in my DB and the information I give contains non-
 ascii characters. I used {{{__icontains}}} for the search. This leads to a
 unicode error in django.
 
 This is the View that is used to search into DB:
 
 ----
 
 {{{
 
 
 
 def search_entreprise(request):
     form = EntrepriseSearchForm()
     if request.method=='POST':
         new_data = request.POST.copy()
         form = EntrepriseSearchForm(new_data)
         if form.is_valid():
             data = form.clean_data
             entreprise_list = Entreprise.objects.all()
             if data['domaine']:
 entreprise_list=entreprise_list.filter(domaine=data['domaine'])
             if data['entrepriseMere']:
 entreprise_list=entreprise_list.filter(entrepriseMere=data['entrepriseMere'])
             if data['nom']:
 entreprise_list=entreprise_list.filter(nom__icontains=data['nom'])
             return
 render_to_response('stagesECL/entreprise_list.html',{'list':entreprise_list,})
     return
 render_to_response('stagesECL/entreprise_search.html',{'form':form,})
 }}}
 ----
 
 where in this case the only information given is the entreprise name (nom)
 which had the value "hé".
 
 Here is the error I get:
 
 ----
 
 {{{
 
 UnicodeEncodeError at /entreprise/search/
 'ascii' codec can't encode character u'\xe9' in position 1: ordinal not in
 range(128)
 Request Method:         POST
 Request URL:    http://localhost/entreprise/search/
 Exception Type:         UnicodeEncodeError
 Exception Value:        'ascii' codec can't encode character u'\xe9' in
 position 1: ordinal not in range(128)
 Exception Location:     c:\Python25\lib\site-
 packages\django\db\models\fields\__init__.py in , line 25
 }}}
 ----
 
 And finally this is the backtrace:
 
 
 ----
 
 
 {{{
 
 Traceback (most recent call last):
 File "c:\Python25\lib\site-packages\django\template\__init__.py" in
 render_node
   712. result = node.render(context)
 File "c:\Python25\lib\site-packages\django\template\defaulttags.py" in
 render
   100. len_values = len(values)
 File "c:\Python25\lib\site-packages\django\db\models\query.py" in __len__
   100. return len(self._get_data())
 File "c:\Python25\lib\site-packages\django\db\models\query.py" in
 _get_data
   430. self._result_cache = list(self.iterator())
 File "c:\Python25\lib\site-packages\django\db\models\query.py" in iterator
   171. select, sql, params = self._get_sql_clause()
 File "c:\Python25\lib\site-packages\django\db\models\query.py" in
 _get_sql_clause
   444. joins2, where2, params2 = self._filters.get_sql(opts)
 File "c:\Python25\lib\site-packages\django\db\models\query.py" in get_sql
   574. joins2, where2, params2 = val.get_sql(opts)
 File "c:\Python25\lib\site-packages\django\db\models\query.py" in get_sql
   622. return parse_lookup(self.kwargs.items(), opts)
 File "c:\Python25\lib\site-packages\django\db\models\query.py" in
 parse_lookup
   743. joins2, where2, params2 = lookup_inner(path, lookup_type, value,
 opts, opts.db_table, None)
 File "c:\Python25\lib\site-packages\django\db\models\query.py" in
 lookup_inner
   915. params.extend(field.get_db_prep_lookup(lookup_type, value))
 File "c:\Python25\lib\site-packages\django\db\models\fields\__init__.py"
 in get_db_prep_lookup
   172. return ["%%%s%%" % prep_for_like_query(value)]
 File "c:\Python25\lib\site-packages\django\db\models\fields\__init__.py"
 in
   25. prep_for_like_query = lambda x: str(x).replace("\\",
 "\\\\").replace("%", "\%").replace("_", "\_")
 
   UnicodeEncodeError at /entreprise/search/
   'ascii' codec can't encode character u'\xe9' in position 1: ordinal not
 in range(128)
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/3412>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to