Enable Markdown to support Django1.6 and code syntax highlighting

Source: Internet
Author: User
Tags python mysql django tutorial

Enable Markdown to support Django1.6 and code syntax highlighting

After the Django project is upgraded to 1.6, django is found. contrib. markup has been removed. It is not recommended as early as 1.5 and marked as deprecated. if you want to continue using the markdown syntax, you can customize a filter.

#my_markup.pymport markdownfrom django import templatefrom django.template.defaultfilters import stringfilterfrom django.utils.encoding import force_unicodefrom django.utils.safestring import mark_saferegister = template.Library()@register.filter(is_safe=True)@stringfilterdef my_markdown(value):    extensions = ["nl2br", ]    return mark_safe(markdown.markdown(force_unicode(value),                                       extensions,                                       safe_mode=True,                                       enable_attributes=False))

Save it in the templatetags directory of the app and load it in the template.

{% load my_markup %}{{ value|my_markdown }}

-------------------------------------- Split line --------------------------------------

Because Django1.6 does not support markdown by default, you need to write the filter yourself. A new problem is that the code block is not highlighted by default and looks very monotonous. So I have studied it today.

The markdown module of python has excellent functions. The highlight is that it also supports extension plug-ins. CodeHilite is used for code highlighting extension. Easy to use,

def md1(value):    extensions = ["nl2br", "codehilite"]    return mark_safe(markdown.markdown(force_unicode(value),                                   extensions,                                   safe_mode=True,                                   enable_attributes=False))

In factcodehiliteAdd it to the extensions list. In this case, the div label of the code block of the file rendered by markdown has a class attribute.class="codehilite". But the process is not complete yet. The Code has not been highlighted. The true highlight function is implemented by Pygments.

Therefore, you must installPygmentsThe installation method is also very simple.

pip install pygments

If your character is as bad as mine, you need to download the source code from Bitbucket to complete the installation. Download the package, decompress it, and execute:

python setup.py install

After successful installation, there is still one thing missing. Since the code is highlighted, it must be driven by CSS. pygments does not have a style by default. However, you can use it to generate a default css.

E:\>pygmentize -S default -f html > default.css

Put default.css in the project, and then you can see the highlighted code. If you do not like this style, pygments-css provides many styles for you to choose from.

Django1.8 returns the json string and the content of the json string that receives the post.

How to Use Docker components to develop a Django project?

Install Nginx + uWSGI + Django on Ubuntu Server 12.04

Deployment of Django + Nginx + uWSGI

Django tutorial

Build a Django Python MySQL Linux development environment

Django details: click here
Django's: click here

This article permanently updates the link address:

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.