7

我是 Python/Django 和整体编程的新手。我需要有关 HttpResponseRedirect 的帮助,因为它在我的登录视图中不起作用。它确实从我的主视图文件中工作,但不是我想要的方式。

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

内容类型:文本/html;charset=utf-8 位置:/

用户实际上已登录,但仍停留在同一页面上。因此,如果我手动转到所需的页面,我会看到我已登录。

以下是相关的代码片段。我到处都在使用自己的观点。我希望以这种方式进行练习和更好地理解。

网址.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()

视图.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/')

登录视图.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 - 简单的表单

<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 书籍教程进行了操作,根据它,一切都应该可以正常工作。如您所见,我也尝试在表单中使用“下一个”隐藏字段,但也不起作用。任何帮助,将不胜感激。我想知道我在这里缺少什么。谢谢!

4

1 回答 1

7

您在 html 中看到 "Content-Type: text/html; charset=utf-8 Location: /" 的原因是因为您返回 HttpResponse 对象作为要在响应中呈现的上下文数据的一部分,而不是实际反应。你的注销看起来也有点奇怪。但是要使用您目前拥有的东西:

改变你的观点.py

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

然后更改 LoginView.login 中的视图以始终返回所需的 Response 对象(重定向或要呈现的页面)

def login_view(request):
    # 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})
于 2013-10-29T03:04:32.733 回答