Django的:Htt的presponseRedirect不工作 [英] Django: HttpResponseRedirect not working

查看:201
本文介绍了Django的:Htt的presponseRedirect不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在的Python / Django的新总体规划的。我需要的Htt presponseRedirect帮助,因为它不是从我的观点登录工作。它从我的主要浏览文件工作,但不是我想要的方式。

I'm new at Python/Django and programming overall. I need help with HttpResponseRedirect as it's not working from my Login view. It does work from my main Views file but not the way I want it.

而不是重定向到所需的页面('/'),我只看到这个同一页上:

Instead of redirecting to the desired page ('/'), I only see this on the same page:

的Content-Type:text / html的;字符集= utf-8的位置:/

Content-Type: text/html; charset=utf-8 Location: /

用户实际获取登录,但留在同一页上。所以,如果我手动转至所需的页面,我会看到,我已登录。

The user actually gets logged in but stay on the same page. So, if I manually go to the desired page, I will see that I am logged in.

下面是code的相关部分。我使用无处不在我自己的看法。我想这种方式实践和更好的理解。

Here are the relevant pieces of code. I am using my own views everywhere. I'd like it this way for practice and better understanding.

Urls.py

from django.conf.urls import patterns, include, url
from django.conf.urls.static import static
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.contrib import admin
from backend import settings
admin.autodiscover()

urlpatterns = patterns('',
    url(r'^login/', 'backend.views.login', name = 'login'),
    url(r'^logout/', 'backend.views.logout', name = 'logout'),
    url(r'^$', 'backend.views.dash', name = 'dash'),
    url(r'^admin/', include(admin.site.urls)),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += staticfiles_urlpatterns()

Views.py

Views.py

from dashboard.dashviews import left, right, topright
from authentication.authviews import LoginView, LogoutView
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.contrib.auth.decorators import login_required

@login_required(login_url='/login/')
def dash(request):
    return render(request, 'dash_template.html', 
        {'left': left.dash_left(request), 
         'right': right.dash_right(), 
        'topright': topright.top_right(request)})

def login(request):
    if not request.user.is_authenticated():
        return render(request, 'login.html', {'login': LoginView.login_view(request)})
    else:
        return HttpResponseRedirect('/')

def logout(request):
    return render(request, 'logout.html', {'logout': LogoutView.logout_view(request)}) and HttpResponseRedirect('/login/')

LoginView.py

LoginView.py

from django.contrib import auth
from django.http import HttpResponseRedirect

def login_view(request):
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    user = auth.authenticate(username=username, password=password)
    if user is not None and user.is_active:
        # Correct password, and the user is marked "active"
        auth.login(request, user)
        # Redirect to dashboard
        return HttpResponseRedirect('/')
    else:
    # Show a message     
        return 'Please enter your username and password below.'

login.html的 - 简单的形式

login.html - simple form

<center>

<p>{{ login }}</p>

  {% if form.errors %}
    <p class="error">Sorry, that's not a valid username or password</p>
  {% endif %}


  <form action="./" method="post">
        <table border="0">
    <tr>
    <td>
    <label for="username">Username:</label>
    </td>
    <td>
    <input type="text" name="username" value="" id="username">
    </td>
    </tr>
    <tr>
    <td>
    <label for="password">Password:</label>
    </td>
    <td>
    <input type="password" name="password" value="" id="password">
    </td>
    </tr>
    </table>
    <input type="submit" value="login" />
    <input type="hidden" name="next" value="/" />
    {% csrf_token %}
  </form>

</center>

我跟着这个Django的书教程,并按照它,它都应该工作得很好。正如你所看到的,我也用我的形式'下一个'隐藏字段,也不起作用尝试。任何帮助将是AP preciated。我不知道我在这里失踪。谢谢!

I followed the Django book tutorial on this and according to it, it all should work just fine. As you can see I've also tried using the 'next' hidden field in my form which doesn’t work either. Any help would be appreciated. I wonder what I'm missing here. Thanks!

推荐答案

之所以你看到内容类型:text / html的;字符集= utf-8的位置:/,在你的HTML是因为你返回的Htt presponse对象作为上下文数据的一部分,以在响应,而不是它是实际的响应呈现。你看起来注销也有点怪异。但与你目前有工作:

The reason why your seeing "Content-Type: text/html; charset=utf-8 Location: /" in your html is because you return a HttpResponse object as part of your context data to be rendered in the response instead of it being the actual response. You're logout looks also a little weird. But to work with what you currently have:

更改您的views.py

Change your views.py

if not request.user.is_authenticated():
    # LoginView.login_view will return a HttpResponse object
    return LoginView.login_view(request)
else:
    ...

然后改变你的LoginView.login视图总是返回你想要的Response对象(重定向或要渲染的页面)

Then change the view in your LoginView.login to always return the Response object you want (the redirect or the page you want rendered)

def login_view(request):
    err_msg = None
    # if the request method is POST the process the POST values otherwise just render the page
    if request.method == 'POST':
        username = request.POST.get('username', '')
        password = request.POST.get('password', '')
        user = auth.authenticate(username=username, password=password)
        if user is not None and user.is_active:
            # Correct password, and the user is marked "active"
            auth.login(request, user)
            # Redirect to dashboard
           return HttpResponseRedirect('/')
        else:
            # Show a message     
            err_msg 'Please enter your username and password below.'
    return render(request, 'login.html', {'login': err_msg})

这篇关于Django的:Htt的presponseRedirect不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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