我很新的Django和测试....Django的测试客户端得到回报404,而不是200
我测试我的应用程序,每一次我做threads_page = self.client.get('/threads/1/')
返回404个状态,而不是200(该网址的作品,1是主题ID)。
我发现一个线程有同样的问题,并有问题的答复(Django test client get returns 404 however works in shell),但我仍然不知道如何解决它。 的问题是,在我的意见我有一个参数一个get_object_or_404,但我不知道如何传递参数的测试:
views.py
def threads(request, subject_id):
subject = get_object_or_404(Subject, pk=subject_id)
return render(request, 'forum/threads.html', {'subject': subject})
这是我的测试,现在
def test_check_threads_content_is_correct(self):
threads_page = self.client.get('/threads/1/')
self.assertEqual(threads_page.status_code, 200)
谢谢!
*这是我在Github上的代码的情况下,它可以帮助 https://github.com/IreneG5/we_are_social_forum
使用'reverse()'函数,在你的应用程序的urls.py'和'namespace'中为你的项目urls.py中的url添加'name'。然后'url = reverse(':')'.. –
zaidfazil
我试过了,但现在我收到了更多的错误......请问您可以发布完整的代码行吗? 而不是'threads_page = self.client.get('/ threads/1 /')'你建议'threads_page = reverse('threads:threads') 而在threads/urls.py中'url(r'^ threads /(?P \ d +)/ $',forum_views.threads,name ='threads')' 而在myproject/urls.py'url(r'^ threads /(?P \ d +)/ $' forum_views.threads,include('threads /(?P \ d +)/ $。urls',namespace ='threads'))'。 我没有在我的应用程序(线程)url.py之前,一切都在项目下urls.py –
Ire