Django开发微信公众号无法回复消息

需求

用Django开发一个公众号,实现的功能是用户发一条文本消息,我直接返回回去
#代码
我的代码如下:

views.py

# 微信调用测试使用
class WeixinInterfaceView(View):
    def get(self, request):
        # 得到GET内容
        signature = request.GET.get('signature', None)
        timestamp = request.GET.get('timestamp', None)
        nonce = request.GET.get('nonce', None)
        echostr = request.GET.get('echostr', None)
        # 自己的token
        token = 'SvenWeng'  # 这里改写你在微信公众平台里输入的token
        # 字典序排序
        tmpList = [token, timestamp, nonce]
        tmpList.sort()
        tmpstr = '%s%s%s' % tuple(tmpList)
        # sha1加密算法
        tmpstr = hashlib.sha1(tmpstr).hexdigest()

        # 如果是来自微信的请求,则回复echostr
        if tmpstr == signature:
            return render(request, 'get.html', {'str': echostr},
                          content_type='text/plain')

    def post(self, request):
        # str_xml = request.body.decode('utf-8')  # use body to get raw data
        str_xml = smart_str(request.body)
        xml = etree.fromstring(str_xml)  # 进行XML解析
        toUserName = xml.find('ToUserName').text
        fromUserName = xml.find('FromUserName').text
        createTime = xml.find('CreateTime').text
        msgType = xml.find('MsgType').text
        content = xml.find('Content').text  # 获得用户所输入的内容
        msgId = xml.find('MsgId').text
        return render(request, 'reply_text.xml',
                      {'toUserName': toUserName,
                       'fromUserName': fromUserName,
                       'createTime': time.time(),
                       'msgType': msgType,
                       'content': content,
                       },
                      content_type='application/xml'
                      )
reply_text.xml

<xml>
<ToUserName><![CDATA[{{ toUserName }}]]></ToUserName>
<FromUserName><![CDATA[{{ fromUserName }}]]></FromUserName>
<CreateTime>{{ createTime }}</CreateTime>
<MsgType><![CDATA[{{ msgType }}]]></MsgType>
<Content><![CDATA[{{ content }}]]></Content>
</xml>
urls.py

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^grzx/', include('MyBlog.urls', namespace='grzx')),
    url(r'^', include('MyBlog.urls')),
    url(r'^weixin/', csrf_exempt(WeixinInterfaceView.as_view())),
]
urlpatterns += staticfiles_urlpatterns()

我使用测试工具受到返回的结果是这样的

<xml>
<ToUserName><![CDATA[diandianweizixun]]></ToUserName>
<FromUserName><![CDATA[wyb199026]]></FromUserName>
<CreateTime>1452836946.28</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[111111111]]></Content>
</xml>

我这个返回和微信的开发文档要求是一样的,为什么我在公众号上回复提示暂时无法服务?附上微信测试工具返回的结果

请求地址:http://www.ddhbblog.sinaapp.com/weixin/

Connection: keep-alive
Date: Fri, 15 Jan 2016 05:50:13 GMT
Transfer-Encoding: chunked
Set-Cookie: saeut=CkMPGlaYiJVgTmYsBOEuAg==; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/
Via: yq26.pyruntime
Server: nginx
Content-Type: application/xml
<xml>
    <ToUserName>
        <![CDATA[diandianweizixun]]>
    </ToUserName>
    <FromUserName>
        <![CDATA[wyb199026]]>
    </FromUserName>
    <CreateTime>1452837013.23</CreateTime>
    <MsgType>
        <![CDATA[text]]>
    </MsgType>
    <Content>
        <![CDATA[12312312]]>
    </Content>
</xml>

求大神指点迷津

https://www.zhihu.com/question/34501713/answer/60667535

直接HttpReponse(reply_xml) 是可以实现的,(reply_xml='''...'')
reply_xml和render的 content 内容是一样的,但是类型不一样。reply_xml 是str类型, content 是 django.utils.safestring.SafeText 类型。

查看render 的源码,最终也是调用 HttpResponse(content)。 不过因为类型不一样,导致 微信服务器无法解析。

解决办法:1、 修改模板: 去掉<![CDATA[]]> 因为xml中的safetext 和django 中的 safetext 冲突。 (我自己用的是该方法)
2、修改django设置 具体怎么做 参见 django 官方文档 template layer 中 variable 什么的 。{{ variable | safe }} 没有研究。

希望对你有帮助。