I'm very new in django, and currently was following a tutorial and I was trying to test users are listed on user page. So I setup test_admin.py. And i get an AssertionError 302 != 200.
Below are my 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)
and this is my 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)
and this is my error
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'...
The issue could be related to APPEND_SLASH
Django setting. By default its True
. So check your urls file and make sure this url ends with slash /
(like articles/<int:year>/
or otherwise Django won't find the url and will automatically add slash and redirects you to a new url and you will receive 302
status code as redirect.
it's my fault, I accidently set my is_active default into False, it should be like this
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.