博客系统

    一、判断登陆状态

    1、前端实现

     

<div class="info" user_username="{{ request.user.username }}" article_id="{{ article_obj.nid }}"></div>
                   
if ($(".info").attr("user_username")){// 登录成功状态}

 

     2、后端实现

obj=render(request,"article_detail.html",locals())
        
obj.set_cookie("user_username",request.user.username)
return obj
                
                
if($.cookie("user_username")){// 登录成功状态}
                 

 

 

    二、从哪来回哪去

    1、根据location.pathname实现

     

article_detail.html:
if 未登录:location.href="/login/?next="+location.pathname     // /login/?next=/blog/yuan/articles/1
                
login.html:
if (response["is_login"]){
                            
   if(location.search.slice(6)){   // /blog/yuan/articles/1

      location.href=location.search.slice(6)
      }else {location.href="/"
      }

}

 

   2、根据document.referrer

article_detail.html:      
 if 未登录:location.href="/login/"
                     
login.html:
 if (response["is_login"]){
                            
      if(document.referrer){   // /blog/yuan/articles/1

     location.href=document.referrer
    } else {location.href="/"
    }

}

 

    3、根据cookie

    

views.py : 
def articleDetail(request): obj=render(request,"article_detail.html",locals()) obj.set_cookie("next_path",request.path) return obj article_detail.html: if 未登录:location.href="/login/" login.html: if (response["is_login"]){ $.cookie("next_path") if($.cookie("next_path")){ // /blog/yuan/articles/1 location.href=$.cookie("next_path") }else {location.href="/" } }

 

 

     三、计算园龄

from django import template
from django.utils.safestring import mark_safe

register = template.Library()   #register的名字是固定的,不可改变


@register.filter
def yuanling(t):
    import datetime
    user_create_time=datetime.datetime(year=t.year,month=t.month,day=t.day,hour=t.hour,minute=t.minute,second=t.second)
    ret=datetime.datetime.now()-user_create_time
    return mark_safe(ret)

 

posted @ 2017-12-04 07:50  魅力宁波  阅读(179)  评论(0)    收藏  举报