gpt4 book ai didi

django - 登录表单后的 HttpResponseRedirect 未重定向到配置文件

转载 作者:行者123 更新时间:2023-12-04 00:51:40 24 4
gpt4 key购买 nike

我正在尝试获取 Django 站点的本地副本。生产站点在登录时工作正常,但我的本地实例在完成登录表单后不会重定向到个人资料页面。

这是login_page观点:

def login_page(request):
profile_page = HttpResponseRedirect('profile')
if request.user.is_authenticated():
return profile_page
form = LoginForm(request.POST or None)
if request.POST and form.is_valid():
user = form.login(request)

if user:
login(request, user)
return profile_page

return render(request, 'login.html', {'form': form})

这是服务器的调试输出显示的内容:
Performing system checks...

<function home_page at 0x7f77ad696c08>
System check identified no issues (0 silenced).
July 08, 2017 - 03:21:39
Django version 1.9.1, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[08/Jul/2017 03:21:49] "GET / HTTP/1.1" 200 3276
[08/Jul/2017 03:21:50] "GET /login HTTP/1.1" 200 2370
[08/Jul/2017 03:21:57] "POST /login HTTP/1.1" 302 0
[08/Jul/2017 03:21:57] "GET /profile HTTP/1.1" 302 0
[08/Jul/2017 03:21:57] "GET /login?next=/profile HTTP/1.1" 200 2370

以上之后,浏览器留在 http://127.0.0.1:8000/login?next=/profile并且只显示标准登录页面。

同样,相同的代码正在生产中使用相同版本的 Django(虽然通过 gunicorn/nginx 而不是 django-admin runserver 运行),所以这让我觉得我的 Django 配置中有一些我缺少的东西而不是实际代码问题。

urls.py 条目:
from accounts import urls as account_urls
...
url(r'^', include(account_urls)),

帐户/urls.py:
from django.conf.urls import url

import accounts.views

urlpatterns = [
url(r'profile/?$', accounts.views.user_profile_page,
name='user_profile_page'),

配置文件 View (这永远不会被触发 AFICT - 在其中设置断点无济于事):
@login_required
def user_profile_page(request):
"""Returns user home page, with respective user status of surveys."""

print "User profile accessed: %s" % request

// user specific data here

context = {'some': some, 'data': data,
'here': here, }
return render(request, 'accounts/profile.html', context)

也很有趣: resolve_url似乎没有像我期望的那样进行重新映射:
(Pdb) resolve_url('/profile')
'/profile'

那不应该指向 acccounts/profile127.0.0.1:8000/profile或类似的东西?

这是正在执行的 AUTHENTICATION_BACKEND 的 'authenticate' 方法(不确定这与标准 Django 有何不同)。这里的所有答案都暗示 authenticate需要接受 request参数 - 我可以更新此方法以在此处附加一些内容吗?:
def authenticate(self, username=None, password=None, **kwargs):
UserModel = get_user_model()
if username is None:
username = kwargs.get(UserModel.USERNAME_FIELD)
try:
if username is not None:
username = username.lower()

user = UserModel._default_manager.get_by_natural_key(username)
if user.check_password(password):
return user
except UserModel.DoesNotExist:
# Run the default password hasher once to reduce the timing
# difference between an existing and a non-existing user (#20760).
UserModel().set_password(password)

最佳答案

Changed in Django 1.10:In older versions, when you’re manually logging a user in, you must successfully authenticate the user with authenticate() before you call login(). Now you can set the backend using the new backend argument.

If you using Django<=1.10, you must use authenticate method before you login. Otherwise, you have to feed authentication backend at least in login method.Here is the code snippet from django docs.

username = request.POST['username']
password = request.POST['password']
user = authenticate(request, username=username, password=password)
if user is not None:
login(request, user)
# Redirect to a success page.
...
else:
# Return an 'invalid login' error message.
...

关于django - 登录表单后的 HttpResponseRedirect 未重定向到配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44982214/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com