Lesson 26 - Table Views
LEARN & BUILD
Demo Code
- Demo Code
- Follow along with my source code
- Clone my repo and edit the code
Software Process
- Shrinking World Lightweight Plan
- Decide on priorities
- Tasks for next week (1 hour size)
- Test, Fix, Enhance, Improve
Shrinking World Planner
Test
- Use test-driven development
- Run tests
- Fix until everything works
Edit Files
- book/models.py
- book/tests.py
- book/views.py
- book/urls.py
Add Chapters
- Test
- Data
- Views
- Templates
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
- Test
- Manual Tests - list of features to test
- Test Views - List, Detail, Add, Edit, Delete
- Test Data - Create, Read, Update, Delete
Build Chapter Views
- Create views_chapter.py
- Copy code from views_book.py
Python Dictionary
- Associative array with key/value pairs
- Lookup table
```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
- Handy for 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
- Read text from doc files "Documents/01.md"
- Convert to html and save in Chapter object
- Display with Chapter
- Use {% autoescape off %} {{ chapter.html }} {% endautoescape %}
Superhero News - Profile
- Display the hero details on a page
- Show all fields from the Superhero records
Superhero News - Stories
- Show a list of stories for the hero
- Write at least two stories for each hero
- Create a simple article that contains some markdown formatting
- Be creative and have fun
Superhero News - Technical Requirements
- Composite Views
- Style with Bootstrap
- View Inheritance
- Dynamic Menu
- Responsive Design
- Data Fields - strengths, weakness, image