Django从视图重定向到根 [英] Django redirect to root from a view

查看:211
本文介绍了Django从视图重定向到根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个django项目。但是,我遇到了一个小打嗝。我的urls.py看起来像这样

  url(r'^ login /(?P< nextLoc>)$' .views.login'),
url(r'^ logout / $','Home.views.logout'),

我的views.py在Home应用程序中如下所示:

  def login(request, nextLoc):
如果request.method ==POST:
form = AuthenticationForm(request.POST)
user = auth.authenticate(username = request.POST ['username'], password = request.POST ['password'])
如果用户不是没有:
如果user.is_active:
auth.login(请求,用户)
返回重定向(nextLoc )
else:
error ='该帐户已被管理员禁用。联系管理员以启用所述帐户'
else:
error ='用户名/密码对不正确。

else:
如果request.user.is_authenticated():
返回重定向(/ profile /)
form = AuthenticationForm()
error =''
return render_to_response('login.html',{'FORM':form,'ERROR':error},context_instance = RequestContext(request))

def logout(request):
auth.logout(request)
return redirect('/')

现在,当我进入登录页面时,它按预期开放。提交表单后,我收到一条错误,表示找不到模块的URL。在挖了一下之后,我注意到重定向(/)实际上转换为 http:// localhost / login / 而不是 http: //本地主机/ 。在注销时也会发生这种情况,即尝试打开url http:// localhost / logout / 而不是 http:// localhost / 。基本上,当打开的页面是 http:// localhost / login 时, redirect('/')添加/到当前URL的结尾,瞧 - 我得到一个我没想到的URL - http:// localhost / login / 。我无法使用重定向将其重定向到网站的根目录。



请帮助我,如果可能,还可以解释Django这种不合理行为的原因

解决方案

如果您查看重定向文档,有几件事情可以传递给函数:




  • 一个模型

  • 视图名称

  • 一个URL



一般来说,我认为重定向到视图名称而不是URL更好。在你的情况下,假设你的urls.py有一个条目,如下所示:

  url(r'^ $',' Home.views.index'),

我会改用这样的重定向:

  redirect('Home.views.index')


I am creating a django project. However, I have come across a small hiccup. My urls.py looks like this

url(r'^login/(?P<nextLoc>)$', 'Home.views.login'),
url(r'^logout/$', 'Home.views.logout'),

My views.py in the Home app is as follows:

def login(request,nextLoc):
    if request.method == "POST":
        form = AuthenticationForm(request.POST)
        user=auth.authenticate(username=request.POST['username'],password=request.POST['password'])
        if user is not None:
            if user.is_active:
                auth.login(request, user)
                return redirect(nextLoc)
            else:
                error='This account has been disabled by the administrator. Contact the administrator for enabling the said account'
        else:
            error='The username/password pair is incorrect. Check your credentials and try again.'

    else:
        if request.user.is_authenticated():
            return redirect("/profile/")
        form = AuthenticationForm()
        error=''
    return render_to_response('login.html',{'FORM':form,'ERROR':error},context_instance=RequestContext(request))

def logout(request):
    auth.logout(request)
    return redirect('/')

Now when I am going to the login page, it is opening up as expected. After I submit the form, I get an error that says that it can't find the module urls. After digging around a bit, I noticed that the redirect("/") actually translates into http://localhost/login/ instead of http://localhost/. The same happens in logout, i.e. it tries opening the url http://localhost/logout/ instead of http://localhost/. Basically, when the page opened is http://localhost/login, the redirect('/') adds the / to the end of the current url, and voila - I get a url that I didn't expect - http://localhost/login/. I cannot get it to redirect to the root of the site using the redirect.

Please help me out with this and if possible also explain the cause of this irrational behavior of Django

解决方案

If you look at the documentation for redirect, there are several things you can pass to the function:

  • A model
  • A view name
  • A URL

In general, I think it's better to redirect to a view name rather than a URL. In your case, assuming your urls.py has an entry that looks something like:

url(r'^$', 'Home.views.index'),

I would instead use redirect like this:

redirect('Home.views.index')

这篇关于Django从视图重定向到根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆