使用Python Django框架创建一个小型博客
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Python Django框架创建一个小型博客相关的知识,希望对你有一定的参考价值。
---恢复内容开始---
登录页面:
注册页面:
首页综合页面:
使用基于ListView的通用视图,获取article_list,category_list,popular_articles,views_articles等数据对index页面进行渲染的到首页综合页面。
使用基于ListView类的通用视图函数ListView渲染,当传入category_id是则通过category外键过滤出同一category的文章作为article_list渲染category_detail.html页面。
如果不传入category_id则将所有的文章查询来作为article_list,渲染index.html页面。从而实现首页综合页面和category分类页面。
文章分类页面:
,
具体实现代码如下:
1 class IndexView(ListView): 2 context_object_name = "article_list" 3 template_name = "index.html" 4 5 def get_queryset(self): 6 self.request.session["current_url"] = self.request.path#获取当前页面url存入session中,如果文章只是刷新页面,而页面不跳转,则阅读数不增加。 7 if self.template_name == "category_list.html": 8 article_list=Article.objects.filter(category=self.kwargs[‘cate_id‘],status=‘p‘)#如果传入category_id则获取对应分类文章作为article_list 9 else: 10 article_list = Article.objects.filter(status = ‘p‘)#如果不传入category_id则获取所有文章作为article_list 11 for articles in article_list: 12 articles.body = markdown2.markdown(articles.body)#文章body部分进行markdown渲染 13 return article_list 14 15 def get_context_data(self,**kwargs): 16 new_comment_list = [] 17 try: 18 kwargs[‘category_name‘]=Category.objects.get(id=self.kwargs[‘cate_id‘]).name 19 except: 20 pass 21 try: 22 user_name = self.request.session[‘username‘] 23 user = get_object_or_404(User,name=user_name) 24 new_comment_list = Comment.objects.filter(comment_reminder=user,comment_status="N") 25 except KeyError: 26 pass 27 comment_count = len(new_comment_list) 28 kwargs[‘comment_list_user‘] = new_comment_list 29 kwargs[‘comment_count_user‘] = comment_count 30 kwargs[‘latest_login_user‘]=User.objects.all().order_by(‘-last_login_time‘)[:5] 31 kwargs[‘category_list‘]=Category.objects.all().order_by(‘name‘) 32 kwargs[‘popular_article‘]=Article.objects.all().order_by(‘likes‘)[:5] 33 kwargs[‘views_article‘]=Article.objects.all().order_by(‘-views‘)[:5] 34 kwargs[‘user_count‘]=User.objects.all().count() 35 kwargs[‘article_count‘]=Article.objects.all().count() 36 return super(IndexView,self).get_context_data(**kwargs)
发布文章页面:
使用视图函数处理表单数据,发布文章代码如下:
1 def article_publish(request): 2 if request.method == "POST": 3 user_name = request.session[‘username‘] 4 user = User.objects.get(name=user_name) 5 article_title = request.POST.get("title") 6 abstract = request.POST.get("abstract") 7 article_body = request.POST.get("content") 8 catogory_name = request.POST.get("category") 9 category = Category.objects.get(name=catogory_name) 10 article_status = request.POST.get("status") 11 article_body = markdown2.markdown(article_body) 12 Article.objects.create(title=article_title,body=article_body,status=article_status,abstract=abstract,auther=user,category=category) 13 14 article_list = Article.objects.filter(status = ‘p‘) 15 for articles in article_list: 16 articles.body = markdown2.markdown(articles.body,) 17 user_count = User.objects.all().count() 18 article_count = Article.objects.all().count() 19 latest_login_user = User.objects.all().order_by(‘-last_login_time‘)[:5] 20 category_list = Category.objects.all().order_by(‘name‘) 21 popular_article = Article.objects.all().order_by(‘-likes‘)[:5] 22 views_article = Article.objects.all().order_by(‘views‘)[:5] 23 new_comment_list = [] 24 new_comment_list = Comment.objects.filter(comment_reminder=user,comment_status="N") 25 comment_count_user = len(new_comment_list) 26 return render(request,"index.html",{"comment_list_user":new_comment_list,"comment_count_user":comment_count_user,"latest_login_user":latest_login_user,"user_count":user_count,"article_count":article_count,"article_list":article_list,"category_list":category_list,"popular_article":popular_article,"views_article":views_article}) 27
个人日志分类页面这个为点击个人日志分类传入个人日志分类的category_id从而获取到对应category的article_list进行渲染得到的:
文章详情页:使用基于DetailView类的通用视图根据点击对应文章的标题传入的article_id获取对应的article object从而渲染出文章详情页面。
文章详情页面代码如下:
1 class ArticleDetailView(DetailView): 2 model = Article 3 template_name = "article_detail.html" 4 context_object_name = "article" 5 pk_url_kwarg = ‘article_id‘ 6 7 8 def get_queryset(self): 9 if self.request.session["current_url"] != self.request.path: 10 a = Article.objects.get(id=self.kwargs[‘article_id‘]) 11 article_url = a.get_absolute_url() 12 a.views = a.views+1 13 a.save() 14 self.request.session["history_url"],self.request.session["current_url"] = self.request.session["current_url"],self.request.path 15 article_list = Article.objects.filter(status = ‘p‘) 16 for articles in article_list: 17 articles.body = markdown2.markdown(articles.body,) 18 return article_list 19 20 # 指定以上几个属性,已经能够返回一个DetailView视图了,为了让文章以markdown形式展现,我们重写get_object()方法。 21 def get_object(self): 22 obj = super(ArticleDetailView, self).get_object() 23 obj.body = markdown2.markdown(obj.body) 24 return obj 25 26 def get_context_data(self,**kwargs): 27 article = Article.objects.get(id=self.kwargs[‘article_id‘]) 28 new_comment_list = [] 29 30 try: 31 user = get_object_or_404(User,name=self.request.session[‘username‘],passwd = self.request.session[‘password‘]) 32 likes_user = article.likes_user.filter(name=self.request.session[‘username‘]) 33 34 if user in likes_user: 35 kwargs[‘in_likes_user‘]=‘Y‘ 36 new_comment_list = Comment.objects.filter(comment_reminder=user,comment_status="N") 37 except KeyError: 38 pass 39 40 users_likes = "" 41 comment_list = Comment.objects.filter(article=article) 42 comment_count = len(comment_list) 43 44 for users in article.likes_user.all().order_by(‘-last_login_time‘)[:5]: 45 users_likes = users_likes+users.name+‘,‘ 46 user_likes_count = article.likes_user.all().count() 47 users_likes = users_likes[:-1] 48 49 comment_count_user = len(new_comment_list) 50 kwargs[‘comment_list_user‘] = new_comment_list 51 kwargs[‘comment_count_user‘] = comment_count_user 52 kwargs[‘comment_count‘] = comment_count 53 kwargs[‘comment_list‘] = comment_list 54 kwargs[‘latest_login_user‘]=User.objects.all().order_by(‘-last_login_time‘)[:5] 55 kwargs[‘user_likes_count‘] = user_likes_count 56 kwargs[‘users_likes‘] = users_likes 57 kwargs[‘user_count‘]=User.objects.all().count() 58 kwargs[‘article_count‘]=Article.objects.all().count() 59 kwargs[‘category_list‘]=Category.objects.all().order_by(‘name‘) 60 kwargs[‘popular_article‘]=Article.objects.all().order_by(‘likes‘)[:5] 61 kwargs[‘views_article‘]=Article.objects.all().order_by(‘-views‘)[:5] 62 return super(DetailView,self).get_context_data(**kwargs)
点赞效果页面:使用Jquery的click时间调用函数生成一个+1动态效果。然后对相关的文章中的likes字段进行+1处理。这个地方的具体处理是。先判断当前登录的user是否已经在对应了文章的
user_likes。如果已经在列表中则显示一个点击之后出现您已经点过赞了,请勿重复点赞效果的标签。
重复点赞:
模板中实现方法如下:
1 {% if not in_likes_user %} 2 <a href="{%url ‘likes_add_page‘ article.pk%}"><span id = "likes_hit" class="badge list-reply-count hidden-xs">点赞{{article.likes_user.count}}</span></a> 3 {%else%} 4 <a href="#"><span id = "likes_hitted" class="badge list-reply-count hidden-xs">点赞{{article.likes_user.count}}</span></a> 5 {%endif%}
jqury代码如下:
1 <script type="text/javascript"> 2 3 $(function(){ 4 $("#likes_hit").click(function(e){ 5 anp(e); 6 }) 7 $("#likes_hitted").click(function(e){ 8 anpped(e); 9 }) 10 11 }); 12 13 function anp(e){ 14 var n=1; 15 var $i=$("<b>").text("+"+n); 16 var x=e.pageX,y=e.pageY; 17 $i.css({top:y-20,left:x,position:"absolute",color:"#E94F06"}); 18 $("body").append($i); 19 $i.animate({top:y-180,opacity:0,"font-size":"1.4em"},1500,function(){ 20 $i.remove(); 21 }); 22 e.stopPropagation(); 23 } 24 function anpped(e){ 25 var n=‘您已经点过赞,请勿重复点赞‘; 26 var $i=$("<b>").text(n); 27 var x=e.pageX,y=e.pageY; 28 $i.css({top:y-20,left:x,position:"absolute",color:"#E94F06"}); 29 $("body").append($i); 30 $i.animate({top:y-180,opacity:0,"font-size":"1.4em"},1500,function(){ 31 $i.remove(); 32 }); 33 e.stopPropagation(); 34 } 35 </script>
今天先到这里,等明天有时间继续编辑
---恢复内容结束---
以上是关于使用Python Django框架创建一个小型博客的主要内容,如果未能解决你的问题,请参考以下文章
我的第一个 Web 应用程序(Python):使用 CGI 还是 Django 之类的框架?