django中自己写判断是否登录的问题

xingbulai 2009-04-20 09:10:39
def check_login(request):
username = request.session.get('username',None)
if username == '':
return HttpResponseRedirect('/login')


def user_profile:
check_login(request)
...
...

用的是自己的用户表,没有用django的中间件什么的.比如用户没有登录,直接访问了user_profile,那应该怎样写让用户跳转到login好呢?
我这样写的,好像不会跳转,郁闷.
...全文
566 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
newlife210 2009-09-13
  • 打赏
  • 举报
回复
。。。。。。。。
xingbulai 2009-07-18
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 stoneyrh 的回复:]
很想知道楼主在什么情况下要自己定义一个用户表
[/Quote]

主要是不想去弄django用户的那一套,看着老火。。。习惯自己写吧。简单点也行
xingbulai 2009-07-18
  • 打赏
  • 举报
回复
搁了这么久,今天来看这个问题,终于解决了,特别感谢jianpx

回复的都有分哈~!
stoneyrh 2009-05-31
  • 打赏
  • 举报
回复
很想知道楼主在什么情况下要自己定义一个用户表
lixq2000 2009-05-25
  • 打赏
  • 举报
回复
路过学习!
cumtnuist 2009-05-25
  • 打赏
  • 举报
回复
up
jianpx 2009-05-15
  • 打赏
  • 举报
回复
可以把check_login写成decorator形式啊,这样就很方便。
sharp_future 2009-04-21
  • 打赏
  • 举报
回复
直接给楼主代码好了,
def login(request):
if request.GET:
redirect_to = request.GET['next']
else:
redirect_to = '/home/'

manipulator = AuthenticationForm(request)
if request.POST:
errors = manipulator.get_validation_errors(request.POST)
if not errors:
from django.contrib.auth import login
login(request, manipulator.get_user())
request.session.delete_test_cookie()
return HttpResponseRedirect(redirect_to)
else:
errors = {}

request.session.set_test_cookie()
cursor = connection.cursor()
# delete expire_date by sharp
cursor.execute("delete from django_session where expire_date < datetime('now','localtime')")
del_sessions = util.dictfetchall(cursor)
return render_to_response('accounts/login.html', {
'form': forms.FormWrapper(manipulator, request.POST, errors),
'next': redirect_to,
'del_sessions':del_sessions},
context_instance=RequestContext(request))
xingbulai 2009-04-20
  • 打赏
  • 举报
回复
不好意思,自己看错了,还是没能跳转,急啊
xingbulai 2009-04-20
  • 打赏
  • 举报
回复
不好意思,可以跳了,原来是缓存的问题.
但是我这样写仍然觉得很麻烦,要在每个action里都要调用check_login,有没有比较简单又科学的办法呢?

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧