Hello everyone,
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = forms.CommentForm(request.POST or None)
if form.is_valid():
response = self.form_valid(form)
return self._set_cookies(response, form)
...
@transaction.atomic
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.ip = get_remote_addr(self.request)
self.object.save()
return HttpResponseRedirect(self.get_success_url())def _set_cookies(self, response, form):
year = 365 * 24 * 60 * 60
response.set_cookie('nickname', form.cleaned_data['nickname'], max_age=year)
response.set_cookie('url', form.cleaned_data['url'], max_age=year)
response.set_cookie('email', form.cleaned_data['email'], max_age=year)
return response
But if the form.cleaned_data['nickname'] contains Chinese, an exception is raised:
Traceback (most recent call last):
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/wsgiref/handlers.py", line 180, in finish_response
self.write(data)
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/wsgiref/handlers.py", line 274, in write
self.send_headers()
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/wsgiref/handlers.py", line 333, in send_headers
self._write(bytes(self.headers))
File "/usr/local/Cellar/python3/3.5.2_3/Frameworks/Python.framework/Versions/3.5/lib/python3.5/wsgiref/headers.py", line 142, in __bytes__
return str(self).encode('iso-8859-1')
UnicodeEncodeError: 'latin-1' codec can't encode characters in position 227-228: ordinal not in range(256)
So how to save the unicode data into cookie? I have to urlencode it manually?
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/4a5ca63a-e947-4c5a-80eb-bab026b6445a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment