Django: HttpResponseRedirect not working
我是Python / Django和整体编程的新手。我需要有关HttpResponseRedirect的帮助,因为它无法从我的登录视图工作。它确实可以从我的主Views文件工作,但不能按我想要的方式工作。
我没有重定向到所需的页面('/'),而是仅在同一页面上看到此内容:
Content-Type: text/html; charset=utf-8 Location: /
用户实际上已登录,但停留在同一页面上。因此,如果我手动转到所需的页面,将会看到我已登录。
这是相关的代码段。我到处都有自己的看法。我希望通过这种方式进行练习和更好地理解。
Urls.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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-简单表单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <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的书籍教程进行操作,根据它,一切都应该正常工作。如您所见,我还尝试过在表单中??使用"下一个"隐藏字段,但该字段也不起作用。任何帮助,将不胜感激。我想知道我在这里想念的是什么。谢谢!
在HTML中看到" Content-Type:文本/ html; charset = utf-8位置:/"的原因是,您将HttpResponse对象作为上下文数据的一部分返回,以呈现在响应中,而不是 实际反应。 您登出后看起来也有些怪异。 但是要使用您当前拥有的东西:
更改您的views.py
1 2 3 4 5 | if not request.user.is_authenticated(): # LoginView.login_view will return a HttpResponse object return LoginView.login_view(request) else: ... |
然后在LoginView.login中更改视图以始终返回所需的Response对象(重定向或所需呈现的页面)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | 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}) |