views.py
import markdown
from markdown.extensions.toc import TocExtension
def blog_detail(request, article_id, slug):
try:
thisarticle = get_object_or_404(Articles, id=article_id, status='有效')
if thisarticle.url_slug != slug:
return render(request, '404.html')
thisarticle.increase_views()
except Exception as e:
return render(request, '404.html')
md = markdown.Markdown(extensions=[
'markdown.extensions.extra',
'markdown.extensions.codehilite',
# 'markdown.extensions.toc',
TocExtension(slugify=slugify)
])
thisarticle.body = md.convert(thisarticle.body)
context = {
'blog': thisarticle,
'toc': md.toc,
}
return render(request, 'single.html', context=context) # 返回info.html页面
使用Pygments生成css
pygmentize -f html -a .codehilite -S tango > tango.css
-a .codehilite指所有css选择器都具有.codehilite这一祖先选择器
-S default就是指定所需要的样式了,各位可以对各种样式都尝试一下。
> tango.css将内容输出到tango.css文件中
关于样式,可以在python环境中查看
Arithmetic@qingjiaowosuanshujiadeMacBook-Pro MyBlog % python3
Python 3.6.6 (v3.6.6:4cf1f54eb7, Jun 26 2018, 19:50:54)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from pygments.styles import STYLE_MAP
>>> STYLE_MAP.keys()
dict_keys(['default', 'emacs', 'friendly', 'colorful', 'autumn', 'murphy', 'manni', 'monokai', 'perldoc', 'pastie', 'borland', 'trac', 'native', 'fruity', 'bw', 'vim', 'vs', 'tango', 'rrt', 'xcode', 'igor', 'paraiso-light', 'paraiso-dark', 'lovelace', 'algol', 'algol_nu', 'arduino', 'rainbow_dash', 'abap', 'solarized-dark', 'solarized-light', 'sas', 'stata', 'stata-light', 'stata-dark', 'inkpot'])
>>>
在html文件中使用css
<link type="text/css" rel="stylesheet" href="/static/css/tango.css">
我选择的就是tango,效果如下