我下面的测试返回响应 404。如果我将'party:detail'替换为'/party/party/1',现在它将通过。根据文档,我确实正确传递了参数,那么导致问题的原因是什么?
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'])