python - @method_decorator(csrf_exempt) NameError: name 'method_decorator' is not defined -


i following following guide (https://abhaykashyap.com/blog/post/tutorial-how-build-facebook-messenger-bot-using-django-ngrok) on how create chatbot, until part updated views.py. there seems issue method declaration , don't know wrong. other that, code same guide (with imports guide creator forgot add). here error got when trying run server in virtual environment:

(ivanteongbot) ivans-macbook-pro:ivanteongbot ivanteong$ python manage.py runserver performing system checks...  unhandled exception in thread started <function wrapper @ 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     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>     .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' not defined 

the following code have in views.py app directory:

from django.shortcuts import render  # ivanteongbot/fb_ivanteongbot/views.py django.views import generic django.http.response import httpresponse django.views.decorators.csrf import csrf_exempt # add # create 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 handle facebook messages     def post(self, request, *args, **kwargs):         # converts text payload python dictionary         incoming_message = json.loads(self.request.body.decode('utf-8'))         # facebook recommends going through every entry since might send         # multiple messages in single call during high load         entry in incoming_message['entry']:             message in entry['messaging']:                 # check make sure received call message call                 # might delivery, optin, postback other events                  if 'message' in message:                     # print message terminal                     print(message)              return httpresponse() 

this have in urls.py:

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

not sure wrong method declaration coz did googling , seems declared in right way done user guide.

the tutorial you're reading leaves out number of crucial imports, such 1 provides method_decorator:

from django.utils.decorators import method_decorator 

it may follow "view code on github" link, takes more complete file.


Comments