[Django]-Django Load testing using locust giving Csrf token error

4👍

There are two requirements for this to work:

  1. csrftoken should be passed inside the form data as csrfmiddlewaretoken.
  2. Along with X-CSRFToken header, Referer header
    is also required.

The code would then look like:

self.client.post(
    '/check_login/',
    {
        'username': '####',
        'password': '########',
        'csrfmiddlewaretoken': csrftoken
    },
    headers={
        'X-CSRFToken': csrftoken,
        'Referer': self.parent.host + '/check_login/'
    })
👤philoj

2👍

 def on_start(self):
        self.login()

    def login(self):
        # login to the application
        self.client.auth = HTTPBasicAuth('username', 'password')

this is enough for login. no need to update csrftoken in python. 3.6.8

0👍

I had the same problem.

I have Django 1.11 instance and the name of csrftoken is other, in my case is:

csrftoken = response.cookies['csrftoken_myproject']

And I have searched the csrftoken name with the devtools of Chromium browser (for example).

Leave a comment