UNC BACS 350

Web Apps with Python/Django

Logo

Lesson 24 - Document View

LEARN

Complete Your Projects

Today

Document Viewer

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

```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

templates/doc.html

```html {% extends 'theme.html' %}

{% block content %} {% autoescape off %} {{ doc }} {% endautoescape %} {% endblock content %} ```

Add Markdown 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

To Do

To Do - Test

To Do - Fix

To Do - Extend

To Do - Improvements

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

Test

Edit Files

Add Chapters

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

Superhero News - Stories

Superhero News - Technical Requirements

Project 9 - Instructions