|
|
@@ -1,6 +1,9 @@
|
|
|
-from django.shortcuts import render
|
|
|
+from django.shortcuts import render, redirect
|
|
|
from django.http import HttpResponse
|
|
|
from .models import ArticlePost
|
|
|
+from .form import ArticlePostForm
|
|
|
+from django.contrib.auth.models import User
|
|
|
+import markdown
|
|
|
|
|
|
|
|
|
# 视图函数
|
|
|
@@ -8,3 +11,37 @@ def article_list(request):
|
|
|
articles = ArticlePost.objects.all()
|
|
|
context = {'articles': articles}
|
|
|
return render(request, 'article/list.html', context)
|
|
|
+
|
|
|
+
|
|
|
+def article_detail(request, id):
|
|
|
+ articles = ArticlePost.objects.get(id=id)
|
|
|
+ articles.body = markdown.markdown(articles.body,
|
|
|
+ extensions=[
|
|
|
+ 'markdown.extensions.extra',
|
|
|
+ 'markdown.extensions.codehilite',
|
|
|
+ ])
|
|
|
+ context = {'article': articles}
|
|
|
+ return render(request, 'article/detail.html', context)
|
|
|
+
|
|
|
+
|
|
|
+def article_create(request):
|
|
|
+ if request.method == 'POST':
|
|
|
+ article_post_form = ArticlePostForm(data=request.POST)
|
|
|
+ # 判断提交的数据是否满足模型要求
|
|
|
+ if article_post_form.is_valid():
|
|
|
+ # 保存数据,但是暂时不提交到数据库中
|
|
|
+ new_article = article_post_form.save(commit=False)
|
|
|
+ new_article.author = User.objects.get(id=1)
|
|
|
+ # 将新文章保存到数据库中
|
|
|
+ new_article.save()
|
|
|
+ return redirect("article:article_list")
|
|
|
+ # 如果数据不合法,返回错误信息
|
|
|
+ else:
|
|
|
+ return HttpResponse("表单有错误,请重新填写")
|
|
|
+ # 如果用户请求获取数据
|
|
|
+ else:
|
|
|
+ # 创建表单类实例
|
|
|
+ article_post_form = ArticlePostForm()
|
|
|
+ context = {'article_post_form': article_post_form}
|
|
|
+ # 返回模板
|
|
|
+ return render(request, 'article/create.html', context)
|