我的测试下面会返回一个回复404.如果我用'/ party / party / 1'替换'party:detail',现在它将通过。根据文档,我确实正确传递了参数,因此导致问题的原因是什么?
class UrlTests(TestCase):
def test_login_client(self):
User.objects.create_user('buddy', 'me@me.com', 'password')
self.client.login(username='buddy', password='password')
response = self.client.get(reverse('party:detail', kwargs={'pk': '1'}))
self.assertEqual(301, response.status_code)
演示\网址
urlpatterns = [
url(r'^party/', include('party.urls', namespace='party')), ]
方\网址
urlpatterns = [
url(r'^party/(?P<pk>[0-9]+)/$', login_required(PartyView.as_view()), name='detail'), ]
方\观点
class PartyView(generic.DetailView):
model = Party
template_name = 'party/party.html'
def get_queryset(self):
return Party.objects.filter(pk=self.kwargs['pk'])