AssertionError:移动到基于函数的视图时,False不成立

时间:2014-04-09 20:57:43

标签: python django django-models tdd django-views

我正在使用测试驱动开发方法在Django 1.6中创建一个基本博客。

我关注了this tutorial,但我想改用基于函数的视图,因为我刚开始使用Django。

我在测试中遇到此错误,并且我的index.html模板上没有显示任何内容:

self.assertTrue(post.title in response.content)
AssertionError: False is not true

我评论它是否只是那一行,但self.assertTrue def test_index(self): class PostViewTest(LiveServerTestCase):中的所有<html> <head> <title>My Django Blog</title> </head> <body> {% for post in object_list %} <h1>{{ post.title }}</h1> <h3>{{ post.pub_date }}</h3> {{ post.text }} {% endfor %} </body> </html> 测试都发生了这种情况。

#my_site&gt; ingeldow&gt;模板&gt; blogengine&gt; index.html的

from django.test import TestCase, LiveServerTestCase, Client
from django.utils import timezone
from blogengine.models import Post

# Create your tests here.
class PostTest(TestCase):
    def test_create_post(self):
        # Create the post
        post = Post()

        # Set the attributes
        post.title = 'My first post'
        post.text = 'This is my first blog post'
        post.pub_date = timezone.now()

        # Save it
        post.save()

        # Check we can find it
        all_posts = Post.objects.all()
        self.assertEquals(len(all_posts), 1)
        only_post = all_posts[0]
        self.assertEquals(only_post, post)

        # Check attributes
        self.assertEquals(only_post.title, 'My first post')
        self.assertEquals(only_post.text, 'This is my first blog post')
        self.assertEquals(only_post.pub_date.day, post.pub_date.day)
        self.assertEquals(only_post.pub_date.month, post.pub_date.month)
        self.assertEquals(only_post.pub_date.year, post.pub_date.year)
        self.assertEquals(only_post.pub_date.hour, post.pub_date.hour)
        self.assertEquals(only_post.pub_date.minute, post.pub_date.minute)
        self.assertEquals(only_post.pub_date.second, post.pub_date.second)

class AdminTest(LiveServerTestCase):
    fixtures = ['users.json']

    def setUp(self):
        # Create client
        self.client = Client()

    def test_login(self):
        # Get login page
        response = self.client.get('/admin/')

        # Check response code
        self.assertEquals(response.status_code, 200)

        # Check 'Log in' in response
        self.assertTrue('Log in' in response.content)

        # Log the user in
        self.client.login(username='test', password="password")

        # Check response code
        response = self.client.get('/admin/')
        self.assertEquals(response.status_code, 200)

        # Check 'Log out' in response
        self.assertTrue('Log out' in response.content)

    def test_logout(self):
        # Log in
        self.client.login(username='test', password="password")

        # Check the response code
        response = self.client.get('/admin/')
        self.assertEquals(response.status_code, 200)

        # Check 'Log out' in response
        self.assertTrue('Log out' in response.content)

        # Log out
        self.client.logout()

        # Check response code
        response = self.client.get('/admin/')
        self.assertEquals(response.status_code, 200)

        # Check 'Log in' in response
        self.assertTrue('Log in' in response.content)

    def test_create_post(self):
        # Log in
        self.client.login(username='test', password="password")

        # Check response code
        response = self.client.get('/admin/blogengine/post/add/')
        self.assertEquals(response.status_code, 200)

        # Create the new post
        response = self.client.post('/admin/blogengine/post/add/', {
            'title': 'My first post',
            'text': 'This is my first post',
            'pub_date_0': '2013-12-28',
            'pub_date_1': '22:00:04'
        },
        follow=True
        )
        self.assertEquals(response.status_code, 200)

        # Check added successfully
        self.assertTrue('added successfully' in response.content)

        # Check new post now in database
        all_posts = Post.objects.all()
        self.assertEquals(len(all_posts), 1)

    def test_edit_post(self):
        # Create the post
        post = Post()
        post.title = 'My first post'
        post.text = 'This is my first blog post'
        post.pub_date = timezone.now()
        post.save()

        # Log in
        self.client.login(username='test', password="password")

        # Edit the post
        response = self.client.post('/admin/blogengine/post/1/', {
            'title': 'My second post',
            'text': 'This is my second blog post',
            'pub_date_0': '2013-12-28',
            'pub_date_1': '22:00:04'
        },
        follow=True
        )
        self.assertEquals(response.status_code, 200)

        # Check changed successfully
        self.assertTrue('changed successfully' in response.content)

        # Check post amended
        all_posts = Post.objects.all()
        self.assertEquals(len(all_posts), 1)
        only_post = all_posts[0]
        self.assertEquals(only_post.title, 'My second post')
        self.assertEquals(only_post.text, 'This is my second blog post')

    def test_delete_post(self):
        # Create the post
        post = Post()
        post.title = 'My first post'
        post.text = 'This is my first blog post'
        post.pub_date = timezone.now()
        post.save()

        # Check new post saved
        all_posts = Post.objects.all()
        self.assertEquals(len(all_posts), 1)

        # Log in
        self.client.login(username='test', password="password")

        # Delete the post
        response = self.client.post('/admin/blogengine/post/1/delete/', {
            'post': 'yes'
        }, follow=True)
        self.assertEquals(response.status_code, 200)

        # Check deleted successfully
        self.assertTrue('deleted successfully' in response.content)

        # Check post amended
        all_posts = Post.objects.all()
        self.assertEquals(len(all_posts), 0)

class PostViewTest(LiveServerTestCase):
    def setUp(self):
        self.client = Client()

    def test_index(self):
        # Create the post
        post = Post()
        post.title = 'My first post'
        post.text = 'This is my first blog post'
        post.pub_date = timezone.now()
        post.save()

        # Check new post saved
        all_posts = Post.objects.all()
        self.assertEquals(len(all_posts), 1)

        # Fetch the index
        response = self.client.get('/')
        self.assertEquals(response.status_code, 200)

        # Check the post title is in the response
        self.assertTrue(post.title in response.content)

        # Check the post text is in the response
        self.assertTrue(post.text in response.content)

        # Check the post date is in the response
        self.assertTrue(str(post.pub_date.year) in response.content)
        self.assertTrue(post.pub_date.strftime('%b') in response.content)
        self.assertTrue(str(post.pub_date.day) in response.content)

#my_site&gt; ingeldow&gt; blogengine&gt; tests.py

from django.conf.urls import patterns, include, url
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'ingledow.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

    url(r'^admin/', include(admin.site.urls)),
    url(r'^.*$', include('blogengine.urls')),
)

my_site&gt; ingledow&gt; ingledow&gt; urls.py

from django.conf.urls import patterns, url
from django.views.generic import ListView
from blogengine.models import Post

urlpatterns = patterns('',
        url(r'^$', 'blogengine.views.index'),
    )

my_site&gt; ingeldow&gt; blogengine&gt; urls.py

# Create your models here.
class Post(models.Model):
    title = models.CharField(max_length=200)
    pub_date = models.DateTimeField()
    text = models.TextField()

my_site&gt; ingeldow&gt; blogengine&gt; models.py

来自django.db导入模型

from django.shortcuts import render, get_object_or_404
from blogengine.models import Post

# Create the post list view (david.ingledow.co.uk/blog)
def index(request):
        # get the blog posts that are published
        posts = Post.objects.all
        # now return the rendered template
        return render(request, 'blogengine/index.html', {'post': posts})

my_site&gt; ingeldow&gt; blogengine&gt; views.py

{{1}}

这是我的文件夹结构:

Folder Structure

2 个答案:

答案 0 :(得分:1)

在模板中,帖子列表称为object_list,而在您的视图中,您将其作为post传递。两者需要匹配。

如果您将视图更改为以下内容,我认为应解决此问题:

def index(request):
        # get the blog posts that are published
        posts = Post.objects.all
        # now return the rendered template
        return render(request, 'blogengine/index.html', {'object_list': posts})

此外,您的设置中是否将Debug设置为True?这将启用堆栈跟踪,使这更容易调试,如果找不到模板,它会提出一条消息来确认这一点。

很高兴你喜欢我的教程!

答案 1 :(得分:0)

您是否在settings.py中设置了TEMPLATE_DIRS?它没有找到你的模板。

https://docs.djangoproject.com/en/dev/ref/templates/api/#the-template-dirs-setting