2016-09-01 29 views
2

私はviews.pyを更新した部分まで、チャットボットの作成方法について、以下のガイド(https://abhaykashyap.com/blog/post/tutorial-how-build-facebook-messenger-bot-using-django-ngrok)に従っていました。メソッド宣言に問題があるようで、何が間違っているのか分かりません。それ以外は、コードはほぼガイドと同じです(ガイド作成者が追加しなかった一部のインポートがあります)。ここに私の仮想環境でサーバを実行しようとしたときに私が得たエラーさ:@method_decorator(csrf_exempt)NameError:名前 'method_decorator'が定義されていません

(ivanteongbot) Ivans-MacBook-Pro:ivanteongbot ivanteong$ python manage.py runserver 
Performing system checks... 

Unhandled exception in thread started by <function wrapper at 0x1097a2050> 
Traceback (most recent call last): 
    File "/Users/ivanteong/Envs/ivanteongbot/lib/python2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper 
    fn(*args, **kwargs) 
    File "/Users/ivanteong/Envs/ivanteongbot/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 121, in inner_run 
    self.check(display_num_errors=True) 
    File "/Users/ivanteong/Envs/ivanteongbot/lib/python2.7/site-packages/django/core/management/base.py", line 385, in check 
    include_deployment_checks=include_deployment_checks, 
    File "/Users/ivanteong/Envs/ivanteongbot/lib/python2.7/site-packages/django/core/management/base.py", line 372, in _run_checks 
    return checks.run_checks(**kwargs) 
    File "/Users/ivanteong/Envs/ivanteongbot/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks 
    new_errors = check(app_configs=app_configs) 
    File "/Users/ivanteong/Envs/ivanteongbot/lib/python2.7/site-packages/django/core/checks/urls.py", line 14, in check_url_config 
    return check_resolver(resolver) 
    File "/Users/ivanteong/Envs/ivanteongbot/lib/python2.7/site-packages/django/core/checks/urls.py", line 24, in check_resolver 
    for pattern in resolver.url_patterns: 
    File "/Users/ivanteong/Envs/ivanteongbot/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "/Users/ivanteong/Envs/ivanteongbot/lib/python2.7/site-packages/django/urls/resolvers.py", line 310, in url_patterns 
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) 
    File "/Users/ivanteong/Envs/ivanteongbot/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__ 
    res = instance.__dict__[self.name] = self.func(instance) 
    File "/Users/ivanteong/Envs/ivanteongbot/lib/python2.7/site-packages/django/urls/resolvers.py", line 303, in urlconf_module 
    return import_module(self.urlconf_name) 
    File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name) 
    File "/Users/ivanteong/Desktop/comp9323/ivanteongbot/ivanteongbot/urls.py", line 24, in <module> 
    url(r'^fb_ivanteongbot/', include('fb_ivanteongbot.urls')), 
    File "/Users/ivanteong/Envs/ivanteongbot/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 50, in include 
    urlconf_module = import_module(urlconf_module) 
    File "/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module 
    __import__(name) 
    File "/Users/ivanteong/Desktop/comp9323/ivanteongbot/fb_ivanteongbot/urls.py", line 3, in <module> 
    from .views import IvanTeongBotView 
    File "/Users/ivanteong/Desktop/comp9323/ivanteongbot/fb_ivanteongbot/views.py", line 8, in <module> 
    class IvanTeongBotView(generic.View): 
    File "/Users/ivanteong/Desktop/comp9323/ivanteongbot/fb_ivanteongbot/views.py", line 15, in IvanTeongBotView 
    @method_decorator(csrf_exempt) 
NameError: name 'method_decorator' is not defined 

次のコードは、私は私のアプリディレクトリのviews.pyに持っているものです。

from django.shortcuts import render 

# ivanteongbot/fb_ivanteongbot/views.py 
from django.views import generic 
from django.http.response import HttpResponse 
from django.views.decorators.csrf import csrf_exempt # add this 
# Create your views here. 
class IvanTeongBotView(generic.View): 
    def get(self, request, *args, **kwargs): 
     if self.request.GET['hub.verify_token'] == '2318934571': 
      return HttpResponse(self.request.GET['hub.challenge']) 
     else: 
      return HttpResponse('Error, invalid token') 

    @method_decorator(csrf_exempt) 
    def dispatch(self, request, *args, **kwargs): 
     return generic.View.dispatch(self, request, *args, **kwargs) 

    # Post function to handle Facebook messages 
    def post(self, request, *args, **kwargs): 
     # Converts the text payload into a python dictionary 
     incoming_message = json.loads(self.request.body.decode('utf-8')) 
     # Facebook recommends going through every entry since they might send 
     # multiple messages in a single call during high load 
     for entry in incoming_message['entry']: 
      for message in entry['messaging']: 
       # Check to make sure the received call is a message call 
       # This might be delivery, optin, postback for other events 
       if 'message' in message: 
        # Print the message to the terminal 
        print(message)  
     return HttpResponse() 

これは何ですか私はurls.pyを持っている:

# ivanteongbot/fb_ivanteongbot/urls.py 
from django.conf.urls import include, url # add this 
from .views import IvanTeongBotView 
urlpatterns = [ 
       url(r'^99789126bd00b5454d999cf3a9c3f8a9274d4e1460ac4b9863/?$', IvanTeongBotView.as_view()) 
       ] 

私はいくつかのグーグルを行なったし、私のユーザーガイドを行ったように正しい方法で宣言しているように見えるメソッド宣言だってと間違っているかわかりません。

答えて

5

あなたが読んでいるチュートリアルでは、このようなmethod_decoratorを提供して一つとして重要な輸入品の数を残し:

from django.utils.decorators import method_decorator 

それはあなたを取る「ビューコードgithubの上の」リンクを辿るために役立つかもしれませんmore complete file

+0

これをアプリのviews.pyに追加しました。ありがとう! – iteong

関連する問題