How to solve "user.backend = backend AttributeError: 'NoneType' object has no attribute 'backend" in Django Tests

Viewed 27

I'm new to django 3.6 and tried to write a test that involved the use of force_login. But anytime I run it, it gives me the error user.backend = backend AttributeError: 'NoneType' object has no attribute 'backend' even when my force_login backend parameter is already set to django.contrib.auth.backends.ModelBackend

this is my test code

def test_address_create_stores_user(self):
        user3 = models.User.objects.create_user('user3', 'pw432joij')
        post_data = {
            'name':'john kercher',
            'address1':'1 av st',
            'address2':'',
            'zip_code':'MA12GS',
            'city':'Manchester',
            'country':'uk',
            }
 
        self.client.force_login(user3, backend='django.contrib.auth.backends.ModelBackend')
        self.client.post(reverse('address_create', post_data))
        self.assertTrue(models.Address.objects.filter(user=user3).exists())

and this is the error it gives

*ERROR: test_address_create_stores_user (main.tests.tests_views.TestPage)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\DD\Desktop\practical django\booktime\main\tests\tests_views.py", line 128, in test_address_create_stores_user
    self.client.force_login(user3, backend='hhh')
  File "C:\Users\DD\.virtualenvs\practical_django-UtJdiNPl\lib\site-packages\django\test\client.py", line 600, in force_login
    user.backend = backend
AttributeError: 'NoneType' object has no attribute 'backend'*
0 Answers
Related