I am using Test Driven Development methods to create a basic blog in Django 
1.6.

I followed [this 
tutorial](http://matthewdaly.co.uk/blog/2013/12/28/django-blog-tutorial-the-next-generation-part-1/)
 
but wanted to change to using Function Based Views since I'm just starting 
with Django.

I get this error in my tests and no content shows on my index.html template:

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

I commented it out to see if it was just that line, but it happens to all 
of my `self.assertTrue` tests in `def test_index(self):` which is in `class 
PostViewTest(LiveServerTestCase):`

# # my_site > ingeldow > templates > blogengine > index.html

    <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 > ingeldow > blogengine > tests.py

    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 > ingledow > ingledow > urls.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 > ingeldow > blogengine > 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 > ingeldow > blogengine > models.py

from django.db import models

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

# my_site > ingeldow > blogengine > views.py

    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})

Here's my folder structure:

![Folder Structure][1]


  [1]: http://i.stack.imgur.com/2cp7i.jpg

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/cedab603-d8cc-4d6f-8e4f-ef320b1cf666%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to