2

I am trying test some of the post methods in my Django app, but due to the csrf_token, I can not test it without a browser. So I used @csrf_exempt decorator However I forget to remove those decorators in production. Is there a better way like this?

@csrf_exempt(in_debug_only=True)

So like this decorator is active only when the application is in debug mode. Or is there a better way to test post requests?

Like selenium might have some methods to do that?

Note: I m currently using Django 1.7.2 and Python 3.4.1

geckon
  • 8,316
  • 4
  • 35
  • 59
GIRISH RAMNANI
  • 614
  • 6
  • 18

1 Answers1

2

Inspired by this answer:

You can add a new file to your app, let's call it disable_csrf.py:

class DisableCSRF(object):
    def process_request(self, request):
        setattr(request, '_dont_enforce_csrf_checks', True)

Then you can edit your settings.py to add this:

if DEBUG:
    MIDDLEWARE_CLASSES += myapp.disable.DisableCSRF

Let me know if it worked.

Community
  • 1
  • 1
geckon
  • 8,316
  • 4
  • 35
  • 59