Django Build blog site (iv)

Source: Internet
Author: User
Tags install django pip install django

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-pagedownUsed 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-pagedownbefore using, go to its GitHub address to look at it.

  1. Get the Code:pip install Django-pagedown
  2. Add PageDown to your Installed_apps
  3. 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_previewSet 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.