request.POST.get('resp_162')
will return a unicode
object (unicode string) - or None
but well, that's another problem. There are two ways you can convert it to a str
object (byte string): by passing it to str
- ie str(request.POST.get('resp_162'))
- or by encoding it to a byte string codec using unicode.encode(...)
, ie request.POST.get('resp_162').encode("utf-8")
. The first solution will use the 'ascii' codec, the second will use the codec you ask for.
Since you're first passing your unicode
string to str
and it contains non-ascii characters you get a UnicodeEncodeError
at this point. IOW : just use the second solution and you won't have an error.
This being said : internally, Django only uses unicode strings (for what you get from your models, forms, request etc), and the only sane approach is to stick to unicode strings everywhere (decode the byte strings at system input and encode them to the desired encoding at system output). I don't know what l.answer
is in your snippet but if 'l' (very bad naming choice FWIW) is a model instance and .answer
a text field, it already is a unicode
string so you really shouldn't try to make it a byte string.