如何在测试中使用Django反向?

时间:2022-09-04 11:24:41

I don't know how to use the reverse function in django.I am getting this,

我不知道如何在django中使用反向功能。我得到了这个,

ValueError: dictionary update sequence element #0 has length 1; 2 is required

I could hardcode the urls directly that worked fine, but i wanted to do it in standard way.

我可以直接硬编码工作正常的网址,但我想以标准的方式做到这一点。

main urls.py

 urlpatterns = [
            url(r'^nmailadmin/', admin.site.urls),
            url(r'^api/v1/',include('Core.Login.urls',namespace='login')),
            url(r'^api/v1/mail/',include('Core.Mail.urls',namespace='mail')),
 ]

I have given namespace for login and mail API.

我已经为登录和邮件API提供了命名空间。

When I try to access mail urls like this

当我尝试访问这样的邮件网址时

def test_0list_folders(self):
    url = reverse('mail:folder_list', kwargs={'server':0})
    response = self.client.get('/api/v1/mail/folder_list/0/', follow=True)
    self.assertEqual(status.HTTP_200_OK, response.status_code)

I am getting error like this.

我收到这样的错误。

----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/next/PycharmProjects/Nmail/Nmail-Server/Core/Mail/tests.py", line 25, in test_0list_folders
    url = reverse('mail:folder_list', kwargs={'server':0})
  File "/home/next/venv/NmailVenv/lib/python3.6/site-packages/django/urls/base.py", line 91, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "/home/next/venv/NmailVenv/lib/python3.6/site-packages/django/urls/resolvers.py", line 436, in _reverse_with_prefix
    self._populate()
  File "/home/next/venv/NmailVenv/lib/python3.6/site-packages/django/urls/resolvers.py", line 309, in _populate
    dict(defaults, **pattern.default_kwargs),
ValueError: dictionary update sequence element #0 has length 1; 2 is required

----------------------------------------------------------------------

This is my urls file of mail app.

这是我的邮件应用程序的urls文件。

urlpatterns = [
   url(r'folder_list/(?P<server>\d+)/$', folder_list,name='folder_list'),
   url(r'^auth_login/$', login_auth, name='auth_login'),
   url(r'viewmsg/(?P<server>\d+)/(?P<folder>.*?)/(?P<uid>\d+)/$', viewmsg, name='viewmsg'),
    url(r'action/(?P<action>.*?)/$', action, 'action'),
     ]

Django version : Django==1.11.7

Django版本:Django == 1.11.7

1 个解决方案

#1


0  

My bad. I actually found out that problem is with missing kwarg name in one of my mail app's urls.py

我的错。我实际上发现问题是在我的一个邮件应用程序的urls.py中缺少kwarg名称

urlpatterns = [
   url(r'folder_list/(?P<server>\d+)/$', folder_list,name='folder_list'),
   url(r'^auth_login/$', login_auth, name='auth_login'),
   url(r'viewmsg/(?P<server>\d+)/(?P<folder>.*?)/(?P<uid>\d+)/$', viewmsg, name='viewmsg'),
   url(r'action/(?P<action>.*?)/$', action, name='action'),
     ]

And it all worked fine. Hope this helps someone in future.

这一切都很好。希望这有助于将来的某些人。

P.S : It was already pointed out by an answer Error: "dictionary update sequence element #0 has length 1; 2 is required" on Django 1.4.

P.S:已经通过答案错误指出错误:“Django 1.4上的字典更新序列元素#0的长度为1; 2是必需的”。

#1


0  

My bad. I actually found out that problem is with missing kwarg name in one of my mail app's urls.py

我的错。我实际上发现问题是在我的一个邮件应用程序的urls.py中缺少kwarg名称

urlpatterns = [
   url(r'folder_list/(?P<server>\d+)/$', folder_list,name='folder_list'),
   url(r'^auth_login/$', login_auth, name='auth_login'),
   url(r'viewmsg/(?P<server>\d+)/(?P<folder>.*?)/(?P<uid>\d+)/$', viewmsg, name='viewmsg'),
   url(r'action/(?P<action>.*?)/$', action, name='action'),
     ]

And it all worked fine. Hope this helps someone in future.

这一切都很好。希望这有助于将来的某些人。

P.S : It was already pointed out by an answer Error: "dictionary update sequence element #0 has length 1; 2 is required" on Django 1.4.

P.S:已经通过答案错误指出错误:“Django 1.4上的字典更新序列元素#0的长度为1; 2是必需的”。