2. Not sure... I have a Django application here, and when I search for
"è", it returns the restaurant "Les Deux Frères" without problems.
Could be an encoding problem with your database or with your terminal
or with Python. That's the problem with encoding issues: they take a
lot of time to figure out and properly fix.
Hope this helps.
Vincent.
On Jan 30, 7:56 am, "Sebastien Armand [Pink]" <khe...@gmail.com>
wrote:
Sounds like one is a Unicode object and the other an encoded string
(maybe UTF-8 or Latin-1).
James
--
James Tauber http://jtauber.com/
journeyman of some http://jtauber.com/blog/
What did you do exactly to search, and what is the exact backtrace?
--
noris network AG - Deutschherrnstraße 15-19 - D-90429 Nürnberg -
Tel +49-911-9352-0 - Fax +49-911-9352-100
http://www.noris.de - The IT-Outsourcing Company
> Example with a school and a student class. Each student has a
> foreign key linking to the school where he studies. Through a form,
> I get the informations to create the student including the school's
> id. When I want to create and save the student, am I obliged to
> first hit the database to get the school object and then pass it to
> student(school=school_object) or is there a way to just use the ID
> as it is?
in the database django stores this as school_id - and when you save
you can use that by specificaly setting school_id on the save instead
of school where django will automatically get and insert the id
--
regards
kg
http://lawgon.livejournal.com
http://nrcfosshelpline.in/web/
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 |
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)
___________________________________________
And finally, here's my search view:
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é".
I tried to change the encoding in the html but this doedn't seem to help....
Hope somebody knows how to do this!
I don't see where the exact problem is and whether there's already a
ticket, but this is a bug within the newforms package. It is caused
due to newforms putting unicode into the models, but most parts of
Django are not unicode ready. You can search in the existing tickets
(with category == 'newforms') whether there's already a patch.
If not, could you please open a new ticket and provide exactly the
information from the mail I'm replying to? If you put {{{ and }}}
around the traceback and the code, it won't look as bad as in email
(it's always like this in mail). And please put a link to this
thread's google archive, if you can.
Sebastien Armand [Pink]:
the str(x) throws this exception when x is not an ASCII character.
>
> UnicodeEncodeError at /entreprise/search/
> 'ascii' codec can't encode character u'\xe9' in position 1: ordinal not in
> range(128)
Michael
No, thanks for searching, but it's not. #3387 was triggered by using
queryset.filter(unicode_string)
You didn't use a unicode_string within filter(), though the
traceback is similar. It happened because Django's ORM internal code
looks up the related records to a foreign key with filter(), and
uses a unicode string because because newforms put it in the model.
It's a new bug, but the patch in the #3387 should heal it. Funny.
Please *do* open a ticket. #3387 is closed. I'm now pretty sure that
your case has not been reported before.
> But though, is there another encoding I could use and how do I specify it?
Short answer: No, unless you want to patch Django or python ;-)
The concept of a default encoding is doomed, and python has finally
learned the lesson. I guess that you can use unicode(bytestring)
without giving an encoding at all is a trap that stems from times
when python had a settable default encoding. ASCII is used as a
default encoding in these circumstances. The same applies for
str(unicode_string) or for (bytestring == unicode_string). All these
are traps, traps, traps.
There is a hook in site.py, but this was left in for experiments. If
you use it, you'll probably get into devil's kitchen.
Merci! I finally realized that it's not about a ForeignKey, sorry
for the confusion. Can you check if you can work around it with a
entreprise_list=entreprise_list.filter(domaine=data['domaine'].encode(settings.DEFAULT_CHARSET))
etc.?
Hope that helps!