[PYTHON] Django template reads Makdown and reStructuredText written in an external file as HTML

1. Overview

It's hard to write document pages (Terms of Service, FAQ, Privacy Policy, About Page, etc.) as they are in HTML, so write them in an external file with markdown, load them in a Django template (like the include tag), and use them as HTML. Output.

templates/myapp/about.html


{% load markup %}
{% load my_tags %}

{% read_file 'myapp/about.md' as source %}
{{ source|markdown:"safe" }}

templates/myapp/about.md


#About this app

This app is ...

* Django
* Python
* uwsgi

Output result

スクリーンショット 2016-10-07 11.22.56.png

2. Realization method

1. Install django-markwhat

This time we'll use django-markwhat (+ Markdown) as the core rendering library. If you put this one in, it can support both markdown and reStructuredText, which is convenient.

python


$ pip install django-markwhat Markdown

Added to INSTALLED_APPS

python


  'django_markwhat',

2. Develop tags to search and read files from the template directory

templatetags/my_tags.py


from django import template
from django.template import TemplateDoesNotExist

register = template.Library()


@register.simple_tag(takes_context=True)
def read_file(context, template_name):
    """
Load a text file using the template loader

Example: {% read_file 'admin_doc/pages/about.md' as source %}
    """

    for loader in context.template.engine.template_loaders:
        try:
            source, _path = loader.load_template_source(template_name)
            return source

        except TemplateDoesNotExist:
            pass
    else:
        raise TemplateDoesNotExist(template_name)

Like this, create a template tag that retrieves the contents of the file from the template directory using only the file loading function of Django's template loader.

3. Run

I wrote at the beginning,

templates/myapp/about.html


{% load markup %}
{% load my_tags %}

{% read_file 'myapp/about.md' as source %}
{{ source|markdown:"safe" }}

Markdown is now displayed in HTML.

By the way, Qiita's Markdown is powerful and easy to use, but this Markdown is much poorer.

Recommended Posts

Django template reads Makdown and reStructuredText written in an external file as HTML
Loop the For statement in reverse in an HTML file on Django
[Django] How to read variables / constants defined in an external file
Django HTML Template # 2
Django HTML template
Read logging settings from an external file in Flask
Django returns the contents of the file as an HTTP response
Create an authentication feature with django-allauth and CustomUser in Django
Django # 2 (template)
Django HTML Template # 2
Django Template notes
Django HTML template
Django Template Tips
(Note) Template file search order in Django
File upload with django
Template registration from Django Bootstrap
Show Django ManyToManyField in Template
Use jinja2 template in excel file
Django
Django static file (static) related settings summary
Make a filter with a django template
template
Create a file uploader with Django
How to do arithmetic with Django template
WEB application development using Django [Template addition]
(For myself) Django_1 (Basic / Hello World / Template)
Resolve Angural JS and Django template conflicts
Write a short if-else for Django Template
Django template reads Makdown and reStructuredText written in an external file as HTML