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
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',
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.
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