Django Build blog site (iv)
The last article mainly talk about in the background article editor joins Markdown, already in the article detail page to the markdown parsing.
Django Build blog site (i)
Django Build blog site (ii)
Django Build blog site (iii)
The package you want to use
- Django-pagedown
- Markdown2
django-pagedown
Used to generate the Markdown editor in the background, it markdown2
is used to parse the markdown into HTML displayed on the Web page.
Install
$ pip install django-pagedown$ pip install markdown2
Background Markdown Editor
django-pagedown
before using, go to its GitHub address to look at it.
- Get the Code:pip install Django-pagedown
- Add PageDown to your Installed_apps
- Make sure to collect the static files:
python manage.py collectstatic --noinput
(and if you is working in a development environment, make sure is Prope Rly serving your static files)
You can know that the package is installed and will be django-pagedown
installed in the project:
# settings.py= [ ‘pagedown‘, ‘post.apps.PostConfig‘, ‘django.contrib.admin‘, ‘django.contrib.auth‘, ‘django.contrib.contenttypes‘, ‘django.contrib.sessions‘, ‘django.contrib.messages‘, ‘django.contrib.staticfiles‘,]
But run the above 3rd inside the command, there was an error, the last StackOverflow checked, the original is in the settings.py
inside also have to add the settings:
# settings.py= os.path.dirname(os.path.dirname(os.path.abspath(__file__=‘staticfiles‘= ( ‘static‘),)
It's no problem to run again at this point python manage.py collectstatic --noinput
.
Then the nature is in the background to add Markdown Editor, in fact, only need to let the content of the article support Markdown on the line, that is post
, the model post_content
, modify the admin.py
file is enough:
# admin.py fromDjango.contribImportAdmin from. ModelsImportPost, Posttag fromPagedown.widgetsImportAdminpagedownwidget fromDjangoImportFormsclassPostform (forms. Modelform): Post_content=Forms. Charfield (widget=Adminpagedownwidget (Show_preview=True))classMeta:model=Post fields=' __all__ 'classPostadmin (admin. Modeladmin): Form=Postform Filter_horizontal=(' Posttag ',) Admin.site.register (post,postadmin) admin.site.register (Posttag)
show_preview
Set True
to display a preview when editing.
When you run the project back in the background, you can use Markdown to edit the article content.
Markdown Parsing
When the background uses markdown edit, the contents of the article deposited into the database is markdown code, directly to the page display, is also displayed markdown code.
In Django, each app is allowed to add filters to the template.
This allows the use of filters to markdown2
resolve markdown.
The Django Filter feature, under each app's templatetags
folder, needs to be created by itself, noting that it is created as a package, not a dictionary, which includes a __init__.py
file.
Then, templatetags
create one under the folder djangomarkdown.py
:
# post/templatetags/djangomarkdown.py Import Markdown2from django import template from django.template.defaultfilters import stringfilter from django.utils.safestring import mark_saferegister = template. Library () @register. Filter (Is_safe= true ) @stringfilter def Djangomarkdown (value): return Mark_safe (Markdown2.markdown (value, Extras= [ "fenced-code-blocks" , "cuddled-lists" , , "tables" , "spoiler" ]))
Now that you've created a filter, how do you use it?
In fact, it is directly inside the HTML file can be used, but first in the HTML file loaded (as an post.html
example):
{% extends ' post/base.html '%} {% load Djangomarkdown%} {% block title%}<title>{{Post.post_title}}</title>{% Endblock%} {% block content%}<divclass="Container"> <divclass="Page-header">{{Post.post_title}} <divclass="description"> <spanclass="Col-md-2"><ahref="{% url ' post:user ' Chain '%}">Chain</a></span><spanclass="Col-md-2">{{Post.pub_date}}</span>{% if post.pub_date<Post.change_date%}<spanclass="Col-md-4">Last change:{{Post.change_date}}</span>{% ENDIF%}</div> <br> <divclass="Content">{{Post.post_content|djangomarkdown}}</div> <br> <divclass="Btn-group"role="Group"aria-label="...">{% for tag in post.posttag.all%}<ahref="{% url ' post:tag ' tag.tag_name%}"> <buttontype="button"class="Btn Btn-info">#{{Tag.tag_name}}</button> </a>{% ENDFOR%}</div> </div>{% Endblock%}
First load the custom filter at the beginning of the file, then the HTML can be used directly.
Django Build blog site (iv)