2011-03-29 162 views
0

即时通讯没有得到确切..实际上在httpresponse重定向它什么也没有显示。 实际上http://127.0.0.1:8000/login/在显示我的登录页面 现在我想正确登录它将重定向到我的索引页面。 不确切的方式。django页面重定向成功登录

def login(request): 
    template = "../templates/admin/login.html" 
    data = { 
     } 
    user = auth.authenticate(username='aa', password='bb') 
    if user is not None and user.is_active: 
     template = "../templates/admin/index.html" 

     auth.login(request, user) 
    return HttpResponseRedirect("/login/index/") 

    return render_to_response(template, data, 
           context_instance = RequestContext(request)) 

thanx提前。

+0

http://docs.djangoproject.com/en/1.3/topics/auth/#django.contrib.auth.login – 2011-03-29 13:17:56

+0

看起来是对的,我(假定压痕是在你的版本是正确的)。那会抛出一个python错误,无论如何重定向到'/ login/index /',或者重定向成功。你说它正在返回一个空白页面? – 2011-03-29 13:28:06

+0

实际上,当我写http://127.0.0.1:8000/login/它会自动重定向到索引页面,而不显示登录页面 – gurr 2011-03-29 13:39:27

回答

1

如果您正在自动重定向,那么您的缩进是关闭的。你有不幸的,但是你写的代码没有触发IndentationError

我的猜测是你从文档复制并粘贴并添加到代码中?

我会检查一下,确保没有标签和空格混淆。

这是一个固定的,从POST请求中提取信息。

def login(request): 
    template = "../templates/admin/login.html" 
    data = {} 
    if request.method == 'POST': 
     user = auth.authenticate(username=request.POST.get('username'), 
              password=request.POST.get('password')) 
     if user is not None and user.is_active: 
      auth.login(request, user) 
      return HttpResponseRedirect("/login/index/") 
    return render_to_response(template, data, context_instance = RequestContext(request)) 
+0

不工作stilldef登录(请求): template =“../templates/admin/login html的” 数据= { } 用户= auth.authenticate(用户名= 'AA',口令= 'B-B') 如果用户不是无和user.is_active: auth.login(请求用户) 返回HttpResponseRedirect(“/ login/index /”) return render_to_response(template,data, context_instance = RequestContext(request)) – gurr 2011-03-29 14:42:15

+0

哦,没错。您正在使用硬编码值对用户进行身份验证,因此如果用户'aa'的密码为'bb',您将按照您的设计获得重定向。现在,该从request.POST提取用户名和密码了。 – 2011-03-29 16:18:03