Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fed5214

Browse files
committed
add markdown 语法高亮
1 parent 84e0645 commit fed5214

File tree

10 files changed

+129
-2
lines changed

10 files changed

+129
-2
lines changed
63 Bytes
Binary file not shown.
341 Bytes
Binary file not shown.

‎article/urls.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@
1010
urlpatterns = [
1111
# path函数将url映射到视图
1212
path('article-list/',views.article_list,name='article_list'),
13+
# 文章详情
14+
path('article-detail/<int:id>/',views.article_detail,name='article_detail'),
1315
]

‎article/views.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
# 导入数据模型 ArticlePost
1010
from .models import ArticlePost
11+
import markdown
12+
1113

1214
# 视图函数
1315
def article_list(request):
@@ -18,3 +20,23 @@ def article_list(request):
1820
context = { 'articles': articles}
1921
# render函数 载入模板 并返回context对象
2022
return render(request,'article/list.html',context)
23+
24+
25+
# 文章详情
26+
def article_detail(request,id):
27+
# 取出相应的文章
28+
article = ArticlePost.objects.get(id=id)
29+
30+
# 将markdown 语法渲染成html样式
31+
article.body = markdown.markdown(article.body,
32+
extensions=[
33+
# 包含 缩写 表格等常用扩展
34+
'markdown.extensions.extra',
35+
# 语法高亮扩展
36+
'markdown.extensions.codehilite',
37+
])
38+
39+
# 需要传递给模板的对象
40+
context = {'article': article }
41+
# 载入模板,并返回context对象
42+
return render(request,'article/detail.html',context)

‎db.sqlite3‎

0 Bytes
Binary file not shown.

‎static/md_css/monokai.css‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
.codehilite .hll { background-color: #49483e }
2+
.codehilite { background: #272822; color: #f8f8f2 }
3+
.codehilite .c { color: #75715e } /* Comment */
4+
.codehilite .err { color: #960050; background-color: #1e0010 } /* Error */
5+
.codehilite .k { color: #66d9ef } /* Keyword */
6+
.codehilite .l { color: #ae81ff } /* Literal */
7+
.codehilite .n { color: #f8f8f2 } /* Name */
8+
.codehilite .o { color: #f92672 } /* Operator */
9+
.codehilite .p { color: #f8f8f2 } /* Punctuation */
10+
.codehilite .ch { color: #75715e } /* Comment.Hashbang */
11+
.codehilite .cm { color: #75715e } /* Comment.Multiline */
12+
.codehilite .cp { color: #75715e } /* Comment.Preproc */
13+
.codehilite .cpf { color: #75715e } /* Comment.PreprocFile */
14+
.codehilite .c1 { color: #75715e } /* Comment.Single */
15+
.codehilite .cs { color: #75715e } /* Comment.Special */
16+
.codehilite .gd { color: #f92672 } /* Generic.Deleted */
17+
.codehilite .ge { font-style: italic } /* Generic.Emph */
18+
.codehilite .gi { color: #a6e22e } /* Generic.Inserted */
19+
.codehilite .go { color: #66d9ef } /* Generic.Output */
20+
.codehilite .gp { color: #f92672; font-weight: bold } /* Generic.Prompt */
21+
.codehilite .gs { font-weight: bold } /* Generic.Strong */
22+
.codehilite .gu { color: #75715e } /* Generic.Subheading */
23+
.codehilite .kc { color: #66d9ef } /* Keyword.Constant */
24+
.codehilite .kd { color: #66d9ef } /* Keyword.Declaration */
25+
.codehilite .kn { color: #f92672 } /* Keyword.Namespace */
26+
.codehilite .kp { color: #66d9ef } /* Keyword.Pseudo */
27+
.codehilite .kr { color: #66d9ef } /* Keyword.Reserved */
28+
.codehilite .kt { color: #66d9ef } /* Keyword.Type */
29+
.codehilite .ld { color: #e6db74 } /* Literal.Date */
30+
.codehilite .m { color: #ae81ff } /* Literal.Number */
31+
.codehilite .s { color: #e6db74 } /* Literal.String */
32+
.codehilite .na { color: #a6e22e } /* Name.Attribute */
33+
.codehilite .nb { color: #f8f8f2 } /* Name.Builtin */
34+
.codehilite .nc { color: #a6e22e } /* Name.Class */
35+
.codehilite .no { color: #66d9ef } /* Name.Constant */
36+
.codehilite .nd { color: #a6e22e } /* Name.Decorator */
37+
.codehilite .ni { color: #f8f8f2 } /* Name.Entity */
38+
.codehilite .ne { color: #a6e22e } /* Name.Exception */
39+
.codehilite .nf { color: #a6e22e } /* Name.Function */
40+
.codehilite .nl { color: #f8f8f2 } /* Name.Label */
41+
.codehilite .nn { color: #f8f8f2 } /* Name.Namespace */
42+
.codehilite .nx { color: #a6e22e } /* Name.Other */
43+
.codehilite .py { color: #f8f8f2 } /* Name.Property */
44+
.codehilite .nt { color: #f92672 } /* Name.Tag */
45+
.codehilite .nv { color: #f8f8f2 } /* Name.Variable */
46+
.codehilite .ow { color: #f92672 } /* Operator.Word */
47+
.codehilite .w { color: #f8f8f2 } /* Text.Whitespace */
48+
.codehilite .mb { color: #ae81ff } /* Literal.Number.Bin */
49+
.codehilite .mf { color: #ae81ff } /* Literal.Number.Float */
50+
.codehilite .mh { color: #ae81ff } /* Literal.Number.Hex */
51+
.codehilite .mi { color: #ae81ff } /* Literal.Number.Integer */
52+
.codehilite .mo { color: #ae81ff } /* Literal.Number.Oct */
53+
.codehilite .sa { color: #e6db74 } /* Literal.String.Affix */
54+
.codehilite .sb { color: #e6db74 } /* Literal.String.Backtick */
55+
.codehilite .sc { color: #e6db74 } /* Literal.String.Char */
56+
.codehilite .dl { color: #e6db74 } /* Literal.String.Delimiter */
57+
.codehilite .sd { color: #e6db74 } /* Literal.String.Doc */
58+
.codehilite .s2 { color: #e6db74 } /* Literal.String.Double */
59+
.codehilite .se { color: #ae81ff } /* Literal.String.Escape */
60+
.codehilite .sh { color: #e6db74 } /* Literal.String.Heredoc */
61+
.codehilite .si { color: #e6db74 } /* Literal.String.Interpol */
62+
.codehilite .sx { color: #e6db74 } /* Literal.String.Other */
63+
.codehilite .sr { color: #e6db74 } /* Literal.String.Regex */
64+
.codehilite .s1 { color: #e6db74 } /* Literal.String.Single */
65+
.codehilite .ss { color: #e6db74 } /* Literal.String.Symbol */
66+
.codehilite .bp { color: #f8f8f2 } /* Name.Builtin.Pseudo */
67+
.codehilite .fm { color: #a6e22e } /* Name.Function.Magic */
68+
.codehilite .vc { color: #f8f8f2 } /* Name.Variable.Class */
69+
.codehilite .vg { color: #f8f8f2 } /* Name.Variable.Global */
70+
.codehilite .vi { color: #f8f8f2 } /* Name.Variable.Instance */
71+
.codehilite .vm { color: #f8f8f2 } /* Name.Variable.Magic */
72+
.codehilite .il { color: #ae81ff } /* Literal.Number.Integer.Long */

‎templates/article/detail.html‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{% extends "base.html" %}
2+
{% load staticfiles %}
3+
4+
<!-- 写入base.html 中定义的 title-->
5+
{% block title %}
6+
文章详情
7+
{% endblock title %}
8+
9+
<!-- 写入base.html 中定义的 content-->
10+
{% block content %}
11+
12+
<!-- 文章详情 -->
13+
<div class="container">
14+
<div class="row">
15+
<!-- 标题及作者 -->
16+
<h1 class="col-12 mt-4 mb-4">{{ article.title }}</h1>
17+
<div class="col-12 alert alert-success">作者:{{ article.auther }}</div>
18+
<!--文章正文-->
19+
<div class="col-12">
20+
<!--在 article.body 后加上|safe 过滤器-->
21+
<p>{{ article.body|safe }}</p>
22+
</div>
23+
</div>
24+
</div>
25+
26+
{% endblock content %}

‎templates/article/list.html‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ <h4 class="card-header">{{ article.title }}</h4>
2626
</div>
2727
<!-- 注脚 -->
2828
<div class="card-footer">
29-
<a href="#" class="btn btn-primary">阅读文本</a>
29+
<!-- <a href="#" class="btn btn-primary">阅读本文</a> -->
30+
<a href="{% url 'article:article_detail' article.id %}" class="btn btn-primary">阅读本文</a>
3031
</div>
3132
</div>
3233
</div>

‎templates/base.html‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<title>{% block title%} {% endblock %}</title>
1111
<!-- 引入bootstrap的css文件 -->
1212
<link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap.min.css' %}">
13+
<!-- 引入 monokai.css -->
14+
<link rel="stylesheet" href="{% static 'md_css/monokai.css' %}">
15+
1316
</head>
1417

1518
<body>

‎templates/header.html‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
<ul class="navbar-nav">
1010
<!-- 条目 -->
1111
<li class="nav-item">
12-
<a class="nav-link" href="#">文章</a>
12+
<!--<a class="nav-link" href="#">文章</a> v-->
13+
<a class="nav-link" href="{% url 'article:article_list' %}">文章</a>
1314
</li>
1415
</ul>
1516
</div>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /