【发布时间】:2017-11-04 14:29:45
【问题描述】:
我想在 Django 上练习测试,我有一个想测试的 CreateView。该视图允许我创建一个新帖子,我想检查它是否可以找到没有发布日期的帖子,但首先我测试发布日期的帖子只是为了习惯语法。这就是我所拥有的:
import datetime
from django.test import TestCase
from django.utils import timezone
from django.urls import reverse
from .models import Post, Comment
# Create your tests here.
class PostListViewTest(TestCase):
def test_published_post(self):
post = self.client.post('/post/compose/', {'author':"manualvarado22", 'title': "Super Important Test", 'content':"This is really important.", 'published_date':timezone.now()})
response = self.client.get(reverse('blog:post_detail'))
self.assertContains(response, "really important")
但我明白了:
django.urls.exceptions.NoReverseMatch: Reverse for 'post_detail' with no
arguments not found. 1 pattern(s) tried: ['post/(?P<pk>\\d+)/$']
我如何获得新创建的帖子的 pk?
谢谢!
【问题讨论】:
标签: python django django-testing