On Dec 11, 6:02 am, "Victor Ng" <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> The unicode problem seems to creep up in this list a lot, so here's
> what I've done to solve my problems.
>
> My particular problem is that I need to be able to deal with Unicode
> data in the URLs as well as the regular request GET/POST data.
>
> This is a piece of middleware that I'm using to force all incoming
> data to be UTF-8.  If you also add in a meta tag in your head section
> of your template to declare  utf-8, I think IE will actually do the
> right thing and not do it's weird charset guessing.
>
> Setting that meta tag, along with explicitly seting the
> settings.DEFAULT_CHARSET to 'utf-8', and then applying this middleware
> layer seems to get all my string data inside Django to be clean UTF8
> data.
>
> This has the advantage over a 'full' unicode conversion of Django
> since you don't have to touch any existing Django code, and you don't
> have to enforce non-obvious rules like implementing "__unicode__" and
> using "unicode()" instead of "str()" everywhere.
>
> Anyway, I hope this is of use to people.
>
> The utf8encode function is probably overly paranoid, but well... I
> really don't trust IE to send properly encoded data.
>
> vic
>
>       1 import types
>       2
>       3 '''
>       4 This filter will force any incoming GET or POST data to become
> UTF8 data for
>       5 processing inside of Django.
>       6 '''
>       7
>       8 class UTF8Filter(object):
>       9     def process_request(self, request):
>      10         get_parms = request.GET
>      11         post_parms = request.POST
>      12
>      13         request.GET._mutable = True
>      14         request.POST._mutable = True
>      15
>      16         for cgiargs in [request.GET, request.POST]:
>      17             for key, vallist in cgiargs.items():
>      18                 tmp_values = []
>      19                 if isinstance(vallist, types.ListType):
>      20                     for i, val in enumerate(vallist):
>      21                         tmp_values.append(utf8_encode(val))
>      22                 else:
>      23                     tmp_values = [utf8_encode(vallist),]
>      24
>      25                 cgiargs.setlist(key, tmp_values)
>      26
>      27         # Rewrite the request path as UTF8 data for Ajax calls
>      28         request.path = utf8_encode(request.path)
>      29
>      30         request.GET._mutable = False
>      31         request.POST._mutable = False
>      32
>      33         return None
>      34
>      35 def utf8_encode(val):
>      36     try:
>      37         tmp = val.decode('utf8')
>      38     except:
>      39         try:
>      40             tmp = val.decode('latin1')
>      41         except:
>      42             tmp= val.decode('ascii', 'ignore')
>      43     tmp = tmp.encode('utf8')
>      44     return tmp

What was your motivation to create all this?
The reason I am asking, I suppose my problem
(http://groups-beta.google.com/group/django-users/browse_thread/thread/a9b53db451aa4590)
is somehow related to these issues.


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to