python 错误: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)

参照:http://www.runoob.com/django/django-form.html

做了个表单提交和回显,但是报了以上错误

查资料发现是 python从request取值的是unicode码,需要转成当前文档的编码才能拼接

# -*- coding:utf-8 -*-
from django.http import HttpResponse
from django.shortcuts import render_to_response

def search_form(request):
    return render_to_response('search_form.html')

def search(request):
    request.encoding='utf-8'
    if 'q' in request.GET:
        message = '你搜索的是:'+request.GET['q'].encode('utf-8')
    else:
        message ='你提交空表单'
    return HttpResponse(message)

 

posted @ 2017-09-21 15:51  懒企鹅  阅读(699)  评论(0编辑  收藏  举报