Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django return " 500 INTERNAL SERVER ERROR" when $.post a utf-8 string

Tags:

jquery

django

I met a issue that django always return "500 internal server error" when I tried to post a utf-8 string(e.g. Chinese string). But I tried to post a ascii string, it looks OK. Furthermore, It works OK on my own Archlinux machine, but it can't work on another CentOS server. How can I avoid this issue?

template:

 $(".palcesubm").click(function(){
        var location = $('#detail_address').val()
        $.post('/wechat/locate/select/create/',
                {'location':location}, function(){
                window.location = "/wechat/locate/baidu/";
        })
    });

view:

@csrf_exempt
def create_location(request):
    if request.method == 'GET':
        return render_to_response('create_location.html')
    else:
        print request.POST.get('location')
        request.session['location'] = request.POST.get('location')
        return HttpResponse('success')
like image 217
user1179442 Avatar asked Nov 26 '25 04:11

user1179442


1 Answers

You're probably getting an UnicodeEncodeError because

request.POST.get('location')

returns an unicode object. When you try to print it, Python tries to encode it using the 'ascii' codec and fails because it contains non-ascii chars.

If you really want to print it, use:

print request.POST.get('location').encode('utf-8')

EDIT: more info about encodings in Python: https://docs.python.org/2/howto/unicode.html

like image 136
dukebody Avatar answered Nov 28 '25 16:11

dukebody



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!