始终收到此AssertionError:302!= 200:无法检索内容:响应代码为302(预期为200)

时间:2019-12-01 11:09:17

标签: python django python-3.x tdd

我在django中是一个非常陌生的人,目前正在学习一个教程,我正在尝试测试用户。所以我设置了test_admin.py。我得到一个AssertionError 302!= 200。

下面是我的test_admin.py

from django.test import TestCase,Client
from django.contrib.auth import get_user_model
from django.urls import reverse


class AdminSiteTests(TestCase):

    def setUp(self):
        self.client = Client()
        self.admin_user = get_user_model().objects.create_superuser(
            email='admin@gmail.com',
            password='092100027h'
        )
        self.client.force_login(self.admin_user)
        self.user = get_user_model().objects.create_user(
            email='test@gmail.com',
            password='092100027h',
            name='Test User Full Name'
        )

    def test_users_listed(self):
        """
        Test that users are listed on user page
        """
        url = reverse('admin:core_user_changelist')
        res = self.client.get(url)

        self.assertContains(res, self.user.name)
        self.assertContains(res, self.user.email)

这是我的admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin

from . import models

class UserAdmin(BaseUserAdmin):
    ordering = ['id']
    list_display = ['email', 'name']


admin.site.register(models.User, UserAdmin)

这是我的错误

Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F....
======================================================================
FAIL: test_users_listed (core.tests.test_admin.AdminSiteTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/app/core/tests/test_admin.py", line 28, in test_users_listed
    self.assertContains(res, self.user.name)
  File "/usr/local/lib/python3.7/site-packages/django/test/testcases.py", line 446, in assertContains
    response, text, status_code, msg_prefix, html)
  File "/usr/local/lib/python3.7/site-packages/django/test/testcases.py", line 418, in _assert_contains
    " (expected %d)" % (response.status_code, status_code)
AssertionError: 302 != 200 : Couldn't retrieve content: Response code was 302 (expected 200)

----------------------------------------------------------------------
Ran 5 tests in 0.795s

FAILED (failures=1)
Destroying test database for alias 'default'...

2 个答案:

答案 0 :(得分:1)

该问题可能与m = ~df[['Intermediate1','Intermediate2']].isna().all(axis=1) mask = m.groupby(df['LastName']).transform('any') Django设置有关。默认情况下,其df1 = df[~mask] df2 = df[mask] 。因此,请检查您的网址文件,并确保该网址以斜杠print (df1) LastName Date ObjectCol1 ObjectCol2 ObjectCol3 NumCol1 NumCol2 \ 0 ABC March NaN NaN NaN NaN NaN 1 ABC June NaN NaN NaN NaN NaN 5 AAA March NaN NaN NaN NaN NaN 6 AAA June NaN NaN NaN NaN NaN NumCol3 Intermediate1 Intermediate2 0 NaN NaN NaN 1 NaN NaN NaN 5 NaN NaN NaN 6 NaN NaN NaN print (df2) LastName Date ObjectCol1 ObjectCol2 ObjectCol3 NumCol1 NumCol2 \ 2 XYZ March NaN NaN NaN NaN NaN 3 XYZ June NaN NaN NaN NaN NaN 4 XYZ July NaN NaN NaN NaN NaN 7 DEF March NaN NaN NaN NaN NaN 8 DEF June NaN NaN NaN NaN NaN 9 DEF July NaN NaN NaN NaN NaN NumCol3 Intermediate1 Intermediate2 2 NaN NaN Danger 3 NaN Danger Danger2 4 NaN Danger NaN 7 NaN NaN NaN 8 NaN Danger Danger2 9 NaN Danger NaN 结尾(例如APPEND_SLASH,否则Django无法找到该网址,并且会自动添加斜杠并将您重定向到新的网址,并且您会收到True状态码为重定向。

答案 1 :(得分:1)

这是我的错,我不小心将is_active默认设置为False,应该是这样

class User(AbstractBaseUser, PermissionsMixin):
    """Custom user model that support using email instead of username"""
    email = models.EmailField(max_length=255, unique=True)
    name = models.CharField(max_length=255)
    is_active = models.BooleanField(default=True) <<< This one
    is_staff = models.BooleanField(default=False)