Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

python - 始终收到此AssertionError:302!= 200:无法检索内容:响应代码为302(预期为200)(Always getting this AssertionError: 302 != 200 : Couldn't retrieve content: Response code was 302 (expected 200))

I'm very new in django, and currently was following a tutorial and I was trying to test users are listed on user page.

(我在django中是非常陌生的人,目前正在关注一个教程,并且我正尝试测试用户。)

So I setup test_admin.py.

(所以我设置了test_admin.py。)

And i get an AssertionError 302 != 200.

(我得到一个AssertionError 302!= 200。)

Below are my test_admin.py

(以下是我的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

(这是我的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'...
  ask by metalhaedcoder translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue could be related to APPEND_SLASH Django setting.

(该问题可能与APPEND_SLASH Django设置有关。)

By default its True .

(默认情况下为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.

(因此,请检查您的url文件,并确保此url以斜杠/ (如articles/<int:year>/结尾,否则Django将找不到该url,并会自动添加斜杠并将您重定向到新的url,您将收到302状态代码作为重定向。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...