FunctionGHW posted @ 2014年3月11日 18:11 in 学习杂记 with tags Django Class-based view 微信公众平台 csrf_exempt , 5002 阅读

在SAE上使用Python进行微信公众平台开发,因为SAE支持Django框架,就试着把原先基于web.py的代码换成基于Django的(使用SAE提供的Django v1.4),并使用了Django v1.3才新加的基于类的视图(Class-based view)。View类同样有get和post方法,所以代码做的改动不多。

# coding: utf-8
from django.http import HttpResponse
from django.template import loader, Context
from django.views.generic.base import View

from lxml import etree
import hashlib, time

class WeixinView(View):

    def __init__(self):

    def get(self, request):
        data = request.GET
        signature = data.get('signature')
        timestamp = data.get('timestamp')
        nonce     = data.get('nonce')
        echostr   = data.get('echostr')
        ret = ''
        if self.__validate_wx(signature, timestamp, nonce, echostr):
            ret =  echostr
        return HttpResponse(ret)

    def post(self, request):
        #获得post来的数据 raw string,不能使用request.POST
        str_xml = request.body

        xml = etree.fromstring(str_xml)
        content  = xml.find("Content").text
        msgType  = xml.find("MsgType").text
        fromUser = xml.find("FromUserName").text
        toUser   = xml.find("ToUserName").text

    #微信公众平台请求 验证方法
    def __validate_wx(self, signature, timestamp, nonce, echostr):
        # do validate


一切似乎都正常,然而使用手机端微信进行测试时却收不到任何回应。 几番测试,我发现post方法没有被调用。Google一下,最后找到这个帖子。 看来问题出在CSRF保护机制上。

即使在get和post方法上都加上@csrf_exempt了, 悲剧的发现还是不行<T_T>。是否是因为Class-based view的原因?改成基于函数的view后,果然成功了。 不过我还是想用基于类的视图来处理请求。在Class-based view里是不是需要使用其他的装饰器替换? 搜索答案的时候跑进stackoverflow了,看这里。 看来我还是对基于类的视图这东西不甚了解,查看Django官方文档发现:无论请求是get还是post, 都是先由View类的dispatch方法处理,之后根据请求的method再调用get或post方法。根据这个人的回答,感觉有道理, 于是重写dispatch方法:

# coding: utf-8
from django.views.decorators.csrf import csrf_exempt

class WeixinView(View):

    @csrf_exempt #override
    def dispatch(self, *args, **kwargs):
        return super(WeixinView, self).dispatch(*args, **kwargs)


  • 无匹配
jaco 说:
2015年2月17日 16:20


Nate Mueller 说:
2018年7月15日 15:26

For the message type of the generic into the special forces of the context into importing of the software have the great one. With this response and the loaders that what is the best essay writing service will find the answers on the software by the base of the views.

f88tw┃华歌尔 说:
2020年3月04日 14:47

敬启者:个人小网站希望大家多多支持 感谢您对我们热心的支持 f88tw┃华歌尔┃I appreciate your kind assistance. f88tw|墓园|捡骨流程|捡骨费用|捡骨时间|禁忌|捡骨颜色|捡骨师|新竹|时间|台北|桃园|苗栗|头份|火化|晋塔|安葬|法事|捡骨|看日子|墓穴|墓园|坟墓|看日子|乞丐|

smithseo 说:
2021年5月01日 18:33

Everything has its value. Thanks for sharing this informative information with us. GOOD works! superslot เล่นบนเว็บ

2021年5月04日 22:47

Yes i am totally agreed with this article and i just want say that this article is very nice and very informative article.I will make sure to be reading your blog more. You made a good point but I can't help but wonder, what about the other side? !!!!!!THANKS!!!!!! slotxo เล่นฟรี

2021年5月05日 18:03

This particular papers fabulous, and My spouse and i enjoy each of the perform that you have placed into this. I’m sure that you will be making a really useful place. I has been additionally pleased. Good perform! โฟมล้างหน้าผู้ชาย

SS 说:
2021年5月10日 16:58

You have done a great job on this article. It’s very readable and highly intelligent. You have even managed to make it understandable and easy to read. You have some real writing talent. Thank you. หน้าลอก

SS 说:
2021年5月12日 04:44

It is an excellent blog, I have ever seen. I found all the material on this blog utmost unique and well written. And, I have decided to visit it again and again. บาคาร่า

jackjohnny 说:
2021年6月05日 22:25

Great survey, I'm sure you're getting a great response. Canada turistvisum

jackjohnny 说:
2021年6月17日 22:19

Thank you again for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit. I like visiting you site since I always come across interesting articles like this one.Great Job, I greatly appreciate that.Do Keep sharing! Regards, 먹튀검증

jackjohnny 说:
2021年6月24日 17:34

i really like this article please keep it up.

jackjohnny 说:
2021年6月26日 21:51

I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much. 스포츠토토

jackjohnny 说:
2021年6月29日 18:27

This is my first time visit to your blog and I am very interested in the articles that you serve. Provide enough knowledge for me. Thank you for sharing useful and don't forget, keep sharing useful info: indian business visa

arkseo 说:
2021年7月05日 13:04

All the contents you mentioned in post is too good and can be very useful. I will keep it in mind, thanks for sharing the information keep updating, looking forward for more posts.Thanks how to become a payment service provider

jackjohnny 说:
2021年7月05日 19:06

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. สล็อต

arkseo 说:
2021年7月06日 12:57

I have read your blog it is very helpful for me. I want to say thanks to you. I have bookmark your site for future updates. free backlink checker

jackjohnny 说:
2021年7月11日 18:48

I was reading some of your content on this website and I conceive this internet site is really informative ! Keep on putting up. 토토사이트

Maharashtra 11th Que 说:
2022年8月22日 03:01

Every year, this board administers the 11th grade test, and the Maha 11th Important Question Paper for this examination will soon be available on the Maharashtra Board's official website. Around 15 lakh students took part in the Maharashtra Board Class 11th examinations, which were likewise held in the month of February starting in March 2023. Maharashtra 11th Question Paper 2023 The Maharashtra Board Class 11th Important Question Paper 2023 was released in the month of May 2023. According to a poll, Maharashtra 11th Important Question Paper 2023 is expected to be released in May of this year as well. We advise all students to be patient and wait for the release of the Important Question Paper 2023.

AP SSC Hindi Questio 说:
2022年9月17日 01:57 Every student of class 10 can learn Hindi easily and this is or National language, Hindi is the most important language for every student, AP SSC Hindi Question Paper and the Board of Secondary Education, Andhra Pradesh (BSEAP) class 10th of SSC students also can download the AP 10th Hindi Model Paper 2023 Pdf with Solved question paper in chapter wise for all lessons of the course as AP SSC Hindi Model Paper 2023. Every student of class 10 can learn Hindi easily and this is or National language, Hindi is the most important language for every student.

登录 *

loading captcha image...
or Ctrl+Enter