简体   繁体   中英

How to fix error testing Authentication user?

I'm learning Drf, I'm figuring out to Athuenticate user login Testapi in Drf, it showing error Not valid view function or pattern name. Can Anyone suggest what is wrong with the code?

URLS.PY

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('college.urls')),
    path('auth',include('rest_framework.urls'), name='rest_framework'),


]

TEST.PY

USER_URL = reverse('auth')
class StudentsDetailsTestCase(APITestCase):

    def test_login_user(self):
        self.assertTrue(self.client.login(username='***', 
                        password='***'))
        response = self.client.get(USER_URL)
        self.assertEqual(response.status_code,status.HTTP_200_OK)

traceback error

Traceback (most recent call last):
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\unittest\loader.py", line 436, in _find_test_path
    module = self._get_module_from_name(name)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\unittest\loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "C:\Users\collegedjango\MYSITE\college\tests.py", line 35, in <module>
    USER_URL = reverse('auth')
  File "C:\Users\collegedjango\venv\lib\site-packages\rest_framework\reverse.py", line 47, in reverse
    url = _reverse(viewname, args, kwargs, request, format, **extra)
  File "C:\Users\collegedjango\venv\lib\site-packages\rest_framework\reverse.py", line 60, in _reverse
    url = django_reverse(viewname, args=args, kwargs=kwargs, **extra)
  File "C:\Users\collegedjango\venv\lib\site-packages\django\urls\base.py", line 86, in reverse
    return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
  File "C:\Users\collegedjango\venv\lib\site-packages\django\urls\resolvers.py", line 729, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'auth' not found. 'auth' is not a valid view function or pattern name.


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

you need to use it with rest_framework because of this line path('auth',include('rest_framework.urls'), name='rest_framework'),

you can update reverse by reverse(rest_framework) or you can change line to path('auth',include('rest_framework.urls'), name='auth'),

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM