UNC BACS 350

Web Apps with Python/Django

Logo

Lesson 26 - Table Views

LEARN & BUILD

Demo Code

Software Process

Shrinking World Planner

Test

Edit Files

Add Chapters

Chapter Data Model

Create a representation for Chapters in the Book

python class Chapter(models.Model): book = models.CharField(max_length=200) order = models.IntegerField() title = models.CharField(max_length=200) markdown = models.TextField() html = models.TextField() document = models.CharField(max_length=200)

Test

Build Chapter Views

Python Dictionary

```python d = dict(book='Iliad', title='Achilles', order='1', document='1.md')

d = {'book': 'Iliad', 'title': 'Achilles', 'order': '1', 'document': '1.md'} ```

Passing Keyword Arguments

```python def my_function(**kwargs): title = kwargs['title'] title = kwargs.get('title', 'No title')

my_function(**d) ```

Test with Data

Define a dictionary to create new objects

class ChapterDataTest(TestCase):

    def setUp(self):
        self.chapter1 = dict(book='Iliad', title='Achilles', order='1', document='1.md')
        self.chapter2 = dict(book='Iliad', title='Agamememnon', order='2', document='2.md')

    def test_add_chapter(self):
        Chapter.objects.create(**self.chapter1)
        Chapter.objects.create(**self.chapter2)
        self.assertEqual(len(Chapter.objects.all()), 2)

Read Document

Superhero News - Profile

Superhero News - Stories

Superhero News - Technical Requirements

Project 9 - Instructions