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 factcodehilite
Add 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 installPygments
The 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: