網站效果可點擊這裏訪問html
今天主要完成的是文章在頁面的顯示以及評論,留言前端
首先我但願主頁面是顯示個人全部文章,因而在主頁面的視圖函數中返回了全部的文章對象:vue
def index(request):
if request.method == 'GET':
all_article = models.Article.objects.all().order_by('id').reverse()
all_type = models.ArticleType.objects.all()
return render(request, 'show/index.html',{'all_article':all_article,'all_type':all_type})
all_type爲全部的的文章類別,用於導航條的顯示,以後就能夠遍歷全部的文章對象在主頁顯示了:python
{% extends "show/base.html" %} <!-- ======================== END HEADER AREA HERE ================================= --> <!-- =================== START MAIN CONTENT AREA HERE ========================--> {% block main %} <main class="main-content-area section-padding-bottom"> <div class="container"> <div class="row"> <div class="col-md-12"> <div class="row"> {% for article in all_article %} <div class="col-md-10 mx-auto nagative-margin" data-aos="fade"> <div class="themeix-single-post"> <div class="left-post-number float-left"> <div class="post-number"><span>{{ article.id }}</span></div> </div> <div class="right-blog-details "> <div class="blog-meta"> <p>{{ article.creationTime }}</p> {% for type in article.articletotype_set.all %} <p>{{ type.type.name }}</p> {% endfor %} </div> <div class="blog-title pb-3 pt-1"> <h2 class="heading-2"><a href="/blog-details/?article_id={{ article.id }}">{{ article.title }}</a></h2> </div> <div class="blog-thumb mb-0"> <a href="/blog-details/?article_id={{ article.id }}"><img src="/static/assets/images/article/{{ article.id }}.jpg" alt="post" /></a> </div> </div> </div> </div> {% endfor %} </div> </div> </div> <div class="post-pagination text-center" data-aos="fade-up"> <ul> <li><a href="#"><i class="fa fa-angle-double-left" aria-hidden="true"></i></a></li> <li><a href="#">01</a></li> <li><a href="#">02</a></li> <li><a href="#">03</a></li> <li><a href="#"><i class="fa fa-angle-double-right" aria-hidden="true"></i></a></li> </ul> </div> </div> </main> <!-- ======================== END MAIN CONTENT AREA HERE ========================--> <!-- ==================== START FOOTER AREA ===================================== --> {% endblock %}
因爲只顯示標題有一點空,就是感受少些什麼,因而我在寫文章時添加了配圖,怎麼添加配圖具體就不說了,結果爲:web
在主頁面,能夠點擊進入查看具體的文章,發的是get請求,參數是文章id:django
def blog_details(request):
if request.method == 'GET':
all_type = models.ArticleType.objects.all()
article_id = request.GET.get('article_id')
article_obj = models.Article.objects.filter(id=article_id).first()
return render(request,'show/blog-details.html',{'article_obj':article_obj,'all_type':all_type})
返回的是文章對象,經過這一個文章對象來顯示內容,因爲以前經過markdown模塊渲染成了html,因此文章內容直接顯示就行json
{% extends "show/base.html" %} <!-- ======================== END HEADER AREA HERE ================================= --> <!-- =================== START MAIN CONTENT AREA HERE ========================--> {% block link %} <style> code { color: black; background: none; text-shadow: 0 1px white; font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace; font-size: 1em; text-align: left; white-space: pre; word-spacing: normal; word-break: normal; word-wrap: normal; line-height: 1.5; -moz-tab-size: 4; -o-tab-size: 4; tab-size: 4; -webkit-hyphens: none; -moz-hyphens: none; -ms-hyphens: none; hyphens: none; } </style> {% endblock %} {% block main %} <!-- ======================== END HEADER AREA HERE ================================= --> <!-- =================== START MAIN CONTENT AREA HERE ========================--> <main class="main-content-area section-padding-bottom"> <div class="container"> <div class="row"> <div class="col-md-12"> <div class="row"> <div class="col-md-10 mx-auto n-margin"> <div class="entry-content"> <h1 class="text-center" >{{ article_obj.title }}</h1> {{ article_obj.htmlContent|safe }} </div> <div class="tags-social-meta mb-5 row"> <div class="col-md-6"> <div class="tags"> <span class="float-left"><i class="fa fa-tag" aria-hidden="true"></i> 分類:</span> <ul class="list-inline"> {% for type in article_obj.articletotype_set.all %} <li class="list-inline-item"><a href="#">{{ type.type.name }}</a></li> {% endfor %} </ul> </div> </div> <div class="col-md-6"> </div> </div> <div class="comment-wrapper mb-5"> <div class="section-title pb-3"> <h3 class="heading-3">{{ article_obj.comment_set.all.count }} Comments</h3> </div> <div class="themeix-comments"> {% for comment in article_obj.comment_set.all %} <div class="comments-details"> <div class="author-image"> <a href="author.html"><img src="https://gravatar.com/avatar/{{ comment.email_hash }}?d=wavatar" alt="" /></a> </div> <div class="comment-text pb-5"> <div class="c-title"> <p><a href="#">{{ comment.userName }}</a> <span class="ml-1">{{ comment.creationTime }}</span></p> </div> <div class="c-content"> <p>{{ comment.content }}</p> </div> </div> </div> {% endfor %} </div> </div> <div class="comment-wrapper"> <div class="section-title mb-4 mt-4"> <h3 class="heading-3">Post a Comment</h3> </div> <div class="comment-form"> <form action="/article_comment/" method="post"> <div class="form-row"> <div class="col-md-6"> <div class="input-group"><input name="username" type="text" placeholder="Your Name" /></div> </div> <div class="col-md-6"> <div class="input-group"><input name="email" type="text" placeholder="Your Email" class="float-right" /></div> </div> </div> <div class="form-row"><div class="col-md-12"><div class="form-group"><textarea name="content" placeholder="Type Comment"></textarea></div></div></div> <input type="hidden" name="article_id" value="{{ article_obj.id }}"> <div class="form-row"><button type="submit" class="btn-submit mt-2">Add Comment</button></div> {% csrf_token %} </form> </div> </div> </div> </div> </div> </div> </div> </main> <!-- ======================== END MAIN CONTENT AREA HERE ========================--> <!-- ==================== START FOOTER AREA ===================================== --> {% endblock %} {% block script %} <script> window.onload=function() { var lables = document.querySelectorAll('code') lables.forEach(function (lable) { var ele = document.createElement('pre') lable.parentNode.replaceChild(ele, lable) ele.appendChild(lable) }) $('pre').addClass('language-python mb-60') $('code').addClass('language-python') } </script> {% endblock %}
最下方的js代碼是爲code元素添加pre父元素,這樣可使markdown文章中的代碼部分顯示的更友好,具體頁面爲:markdown
接下來是評論功能,先看一下頁面:app
目前是隻有一級評論內容,評論者須要輸入username,email和content,建立的評論表爲:函數
class Comment(models.Model):
userName = models.CharField(max_length=128)
email = models.CharField(max_length=128)
content = models.TextField()
email_hash = models.CharField(max_length=256)
article = models.ForeignKey(to=Article,on_delete=models.CASCADE)
creationTime = models.DateTimeField(auto_now_add=True)
評論的表單類爲:
class comment_form(forms.Form):
email = forms.EmailField()
username = forms.CharField()
content = forms.CharField()
article_id = forms.IntegerField()
評論的視圖函數爲:
def article_comment(request):
if request.method == 'POST':
form = comment_form(request.POST)
if form.is_valid():
value_dict = form.clean()
username = value_dict['username']
email = value_dict['email']
content = value_dict['content']
article_id = value_dict['article_id']
models.Comment.objects.create(userName=username,email=email,content=content,article_id = article_id,email_hash=hashlib.md5(email.encode('utf-8')).hexdigest())
messages.info(request, '評論成功')
else:
error_msg = form.errors.as_json() # 獲取錯誤信息
error = json.loads(error_msg)
key_list = error.keys()
item = ''
for i in key_list:
item = i
message = error[item][0]['message']
messages.error(request,message)
obj_url = request.META.get('HTTP_REFERER', "/")
return redirect(obj_url)
這裏存儲了email的hash值是爲了應用gravatar.com網站的頭像,它根據郵箱的hash值來顯示不一樣的頭像,經過一個圖片連接就能夠顯示,具體就很少說了
最後是留言部分,先建立留言表:
class Message(models.Model):
content = models.TextField()
creationTime = models.DateTimeField(auto_now_add=True)
只有兩個字段且匿名,這個弄的簡單點就好
留言的處理函數爲:
def saysomethingtome(request):
if request.method == 'POST':
content = request.POST.get('content')
if content:
models.Message.objects.create(content=content)
messages.info(request,'發送成功,感謝支持')
obj_url = request.META.get('HTTP_REFERER', "/")
return redirect(obj_url)
頁面顯示爲:
留言固然是給我本身看的,因此只在後臺顯示了
今天的主要部分就是上面所說的,中間還有不少的地方也作了改善但並無說出來,這裏主要是爲了進行一個主要內容的記錄,同時也能夠提供一下思路,並無記錄的太細,否則有點雜了。明天預計完成留言,評論在後臺的管理,以及文章在前端的分頁顯示。