用Django写博客(三)编写views

接上篇,现在将数据库里面的内容反映到页面上去,我们就大功告成了,

在urls.py中配置映射到特定的处理方法中去。


我的blog.core.urls.py:

urlpatterns = patterns('',
    (r'root/(?P<path>.*)$',
    'django.views.static.serve',
    {'document_root':
    'D:/Aptana Studio Workspace/blog/views/temp'}),
    (r'^essay/(?P<eid>\d+)/$',
    'blog.core.views.essay_details'),
    (r'^search/$','blog.core.views.search'),
    (r'^leavemsg/(?P<eid>\d+)/$',
    'blog.core.views.leave_comment'),
    (r'^(?P<pageNo>\d+)/(?P<etype>\d+)/$',
    'blog.core.views.index'),
    (r'^latest/feed/$', LatestEntriesFeed()),
    (r'^','blog.core.views.index'),
)

#view.py上面有比较详细的注释

from django.shortcuts import render_to_response,get_object_or_404
from blog.core.models import Essay,EssayType,Archive,Comment
from django.core.paginator import Paginator
import datetime
from django.db.models import Q
#欢迎页,pageNo=None,etype=None这些都是从urls正则中读取的
#param如果没有就是None
def index(request,pageNo=None,etype=None,keyword=None):
    try:
        #文章分页后的页数
        pgNo=int(pageNo)
    except:
        pgNo=1
    try:
        etype=int(etype)
    except:
        etype=None  
    if etype:
        #查询该类别的文章,exclude表示not in或者!=  
        datas=Essay.objects.all().
        filter(eType=etype).
        exclude(title='welcome')  
    elif keyword:
        #根据关键字查询,title__contains
        #表示Sql like %+keyword+%
        #Q对象表示Sql关键字OR查询
        datas=Essay.objects.all().
        get(Q(title__contains=keyword)
        |Q(abstract__contains=keyword))
        .exclude(title='welcome')
    else:
        #查询所有文章
        datas=Essay.objects.all().exclude(title='welcome')
    #最近的5篇文章
    recentList=datas[:5]
    #数据分页
    paginator = Paginator(datas, 10)
    if pgNo==0:
        pgNo=1
    if pgNo>paginator.num_pages:
        pgNo=paginator.num_pages
    curPage=paginator.page(pgNo)
    #返回到mian.html模板页
    return render_to_response('main.html',{
            'page':curPage,
            'essay_type':EssayType.objects.all(),
            'pcount':paginator.num_pages,
            'recent':recentList,
            'archives':Archive.objects.all(),
            'welcome':Essay.objects.filter(title='welcome')[0]})


#文章详细信息
def essay_details(request,eid=None):
    #返回文章详细信息或者404页面
    essay=get_object_or_404(Essay,id=eid)
    recentList=Essay.objects.all()[:5]
    #新用户的Session
    if request.session.get('e'+str(eid),True):
        request.session['e'+str(eid)]=False
        #这里可以用一个timer实现,浏览次数保存在内存中,
        #timer定期将浏览次数提交到数据库
        #文章浏览次数+1
        essay.view_count=essay.view_count+1
        essay.save()
    return render_to_response('details.html',{
              'essay':essay,
              'essay_type':EssayType.objects.all(),
              'archives':Archive.objects.all(),
              'date_format':
     essay.pub_date.strftime('%A %B %d %Y').split(),
              'recent':recentList
                                 })


#根据关键字来搜索文章   
def search(request):
    if request.method == 'POST':
        #从POST请求中获取查询关键字
        key=request.POST.get('keyword',None)
        return index(request,keyword=key)
    else:
        return index(request)
   


#存储用户留言信息
def leave_comment(request,eid=None):
    if request.method == 'POST' and eid:
        uname=request.POST.get('uname',None)
        content=request.POST.get('comment',None)
        email=request.POST.get('email',None)
        #不为空   
        if uname and content and email:
            comment=Comment()
            comment.uname=uname
            comment.content=content
            comment.email=email
            comment.pub_date=datetime.datetime.now()
            comment.save()
        return index(request)
    return index(request)
现在已经大功告成了



如果需要rss订阅功能 请看这里的介绍


用Django写博客(四)编写Rss订阅





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值