Lesson 24 - Document View
LEARN
Complete Your Projects
- Project 1 - Setup development tools
- Project 2 - Build a simple app
- Project 3 - Application Hosting
- Project 4 - TemplateView
- Project 5 - Data Models
- Project 6 - Data Model Views
- Project 7 - User Authentication
- Project 8 - Bootstrap Styling
- Project 9 - Custom Views
Today
- Display Markdown in Django Views
- Book Builder - books & chapters
Document Viewer
- Read markdown text from file
- Convert from markdown to HTML
- Use Markdown Converter to produce HTML
- Display within a Django View
- Pass in doc name on URL
Markdown Text Sample
Simple language constructs to replace HTML
``` # Headline ## Subheadline
* Bullet 1
* Bullet 2
[Google](http://google.com)
![](https://shrinking-world.com/static/images/unc/Bear.png)
```
Installing Markdown
bash
$ pip install markdown
Convert Markdown
- Import converter
- Take Markdown and make HTML
```python from markdown import markdown
markdown_text = '# Headline' html_text = markdown(markdown_text) ```
Test for Markdown Formatter
doc/tests.py
```python from django.test import TestCase from markdown import markdown
class DocViewTest(TestCase):
def test_markdown(self):
markdown_text = '# Headline'
html_text = markdown(markdown_text)
self.assertEqual(html_text, '<h1>Headline</h1>')
```
Document Template
- Add variable to view
- Use autoescape in template
templates/doc.html
```html {% extends 'theme.html' %}
{% block content %} {% autoescape off %} {{ doc }} {% endautoescape %} {% endblock content %} ```
Add Markdown to View
- Read Markdown from file
- Convert Markdown to HTML
- Pass HTML to View
doc/tests.py
```python from django.test import TestCase from markdown import markdown
class DocViewTest(TestCase):
def test_doc_view(self):
response = self.client.get('doc')
self.assertContains(response, 'Web Dev')
```
doc/views.py
```python class DocumentView(TemplateView): template_name = 'doc.html'
def get_context_data(self, **kwargs):
markdown_text = open('README.md').read()
return dict(doc=markdown(markdown_text))
```
doc/urls.py
python
urlpatterns = [
path('doc', DocumentView.as_view()),
]
BUILD
Tasks to Do
- Create a to do list
- Test, Fix, Extend, Improve
- Put the file in the project directory
To Do
- Test
- Fix
- Extend
- Improve
To Do - Test
- Setup django tests
- Book and Chapter data
- Book and Chapter views
- Book and Chapter urls
To Do - Fix
- Security on edit operations
To Do - Extend
- Start project and copy code
- Build Book data and views
- Build Chapter data and views
- Show Chapters with markdown
To Do - Improvements
- Move urls into book/urls.py
- Setup and Teardown in Tests
- Design for Data (Book, Chapter)
- Refactor and simplify
Start a Project
$ cd week9 && mkdir BookBuilder
$ django-admin startproject config .
$ python manage.py migrate
$ python manage.py runserver
Browse http://127.0.0.1:8000
Copy Old Project
- Copy from week8/BookBuilder
- book
- templates
- accounts
- config
- urls.py
- settings.py
- static
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.ForeignKey(Book, on_delete=models.CASCADE)
order = models.IntegerField()
title = models.CharField(max_length=200)
markdown = models.TextField()
html = models.TextField()
document = models.CharField(max_length=200)
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