简体   繁体   English

Django-为什么内置的auth登录功能在成功登录URL后没有将有关用户的信息传递给

[英]Django- why inbuilt auth login function not passing info about user to after successful login url

Hi I used the django inbult auth urls and views for my project and now have finished the initial user account creation/login/reset password process. 嗨,我为我的项目使用了django inbult身份验证url和视图,现在已经完成了初始用户帐户创建/登录/重置密码过程。

Now, the user can log in and be redirected to the after successful login url accounts/profile/. 现在,用户可以登录并在成功登录url帐户/配置文件/之后重定向到。

I have several doubts on the django login function. 我对Django登录功能有一些疑问。 For convenience, I've copy paste the django inbuilt login function code below. 为了方便起见,我在下面复制粘贴了django内置的登录功能代码。

@sensitive_post_parameters()
@csrf_protect
@never_cache
def login(request, template_name='registration/login.html',
          redirect_field_name=REDIRECT_FIELD_NAME,
          authentication_form=AuthenticationForm,
          current_app=None, extra_context=None):
    """
    Displays the login form and handles the login action.
    """
    redirect_to = request.REQUEST.get(redirect_field_name, '')

    if request.method == "POST":
        form = authentication_form(request, data=request.POST)
        if form.is_valid():

            # Ensure the user-originating redirection url is safe.
            if not is_safe_url(url=redirect_to, host=request.get_host()):
                redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)

            # Okay, security check complete. Log the user in.
            auth_login(request, form.get_user())

            return HttpResponseRedirect(redirect_to)
    else:
        form = authentication_form(request)

    current_site = get_current_site(request)

    context = {
        'form': form,
        redirect_field_name: redirect_to,
        'site': current_site,
        'site_name': current_site.name,
    }
    if extra_context is not None:
        context.update(extra_context)
    return TemplateResponse(request, template_name, context,
                            current_app=current_app)

My questions are: 我的问题是:

1 Is the REDIRECT_FIELD_NAME in the function set as '/profile/' in django.contrib.auth ? 1函数中的REDIRECT_FIELD_NAME是否在django.contrib.auth设置为“ / profile /”?

I could see this variable is imported from django.contrib.auth 我可以看到此变量是从django.contrib.auth导入的

from django.contrib.auth import REDIRECT_FIELD_NAME, login as auth_login, logout as auth_logout, get_user_model

I don't have any setting for this variable, but after user successfully logged in, the page will be directed to /accounts/profile/ 我没有此变量的任何设置,但是在用户成功登录后,该页面将定向到/ accounts / profile /

2 Has the login function passed the account info about the user? 2登录功能是否已传递有关用户的帐户信息? If yes, how can I access it? 如果是,我如何访问它?

From the code, if user successfully logged in, page will be redirected: return HttpResponseRedirect(redirect_to) 从代码中,如果用户成功登录,页面将被重定向: return HttpResponseRedirect(redirect_to)

in my case, redirected to accounts/profile/ , initially the view for the url was simply a 在我的情况下,重定向到account / profile /,最初,该URL的视图只是一个

HttpResponse("You have logged in successfully")

now when I am trying to implement the view function, I realize that no info about the user has been passed. 现在,当我尝试实现视图功能时,我意识到没有传递有关用户的信息。

I've tried to print request in the view function, but there is no info about the user in the message printed in the server terminal, all I get is a long list of system settings or other info. 我尝试在视图功能中print request ,但是在服务器终端中显示的消息中没有关于用户的信息,我得到的只是一长串的系统设置或其他信息。 However, the login should pass the info of who has just successfully logged in to the successful log in urls right? 但是,登录名应该将谁刚刚成功登录的信息传递给成功登录的网址,对吗?

Thank you very much for explaining. 非常感谢您的解释。

After the login, you can access the user info by referring request.user in views and just {{user}} in templates. 登录后,您可以通过在视图中引用request.user以及仅在模板中引用{{user}}来访问用户信息。 All you need to make sure is you're passing the RequestContext in the HttpResponse for the future request. 您需要确保的是要在HttpResponse传递RequestContext以用于将来的请求。

Yes, REDIRECT_FIELD_NAME is defined in __init__.py of django.contrib.auth which is simply a "next" what you passed from the login form. 是的, REDIRECT_FIELD_NAME是在django.contrib.auth的__init__.py中定义的,这只是您从登录表单传递的"next"

In Django, there are more than one ways to force a user to login. 在Django中,有多种方法可以强制用户登录。 By decorating a view function with @login_required , by calling the build-in login view for an user defined URL and etc., Refer about the login settings variables here . 通过使用@login_required装饰视图函数,调用用户定义的URL的内置登录视图等,请在此处参考登录设置变量。 You'll get some more ideas. 您会得到更多的想法。

Building custom login page . 建立自定义登录页面 That link gives you an example for custom login implementaion. 该链接为您提供了自定义登录实施的示例。 Consider you have decorated a view with @login_required and it's corresponding URL is /login_test/ . 考虑到您已经用@login_required装饰了一个视图,并且其对应的URL是/login_test/ Then the {{next}} context variable in the login form will be rendered with /login_test/ . 然后,将使用/login_test/呈现登录表单中的{{next}}上下文变量。 So after you login, 因此,登录后,

<input type="hidden" name="next" value="{{ next }}" />

This element's value will be taken for redirecting as per the REDIRECT_FIELD_NAME . 该元素的值将根据REDIRECT_FIELD_NAME用于重定向。 Though I suspect that that example is missing the setting of settings.LOGIN_URL to the URL login/ . 尽管我怀疑该示例缺少设置login/settings.LOGIN_URL Never mind, it's being passed as an argument in the decorator itself. 没关系,它在装饰器本身中作为参数传递。

To override this behavior just put following in settings.py of your app : 要覆盖此行为,只需将以下内容放入应用程序的settings.py中:

LOGIN_REDIRECT_URL = "/"

This will redirect to your home page. 这将重定向到您的主页。 You can change this url to preferred url. 您可以将此网址更改为首选网址。

Once the user is redirected to accounts/profile/ the view for that link will be returned. 将用户重定向到帐户/配置文件/后,将返回该链接的视图。 You can access information about the currently logged in user there as per this post by using request.user . 您可以使用request.user按照此帖子访问有关当前登录用户的信息。 Also tip to see what information you have access to in your views. 还提示您在视图中可以访问哪些信息。 Use import pbd; pdb.set_trace() 使用import pbd; pdb.set_trace() import pbd; pdb.set_trace() . import pbd; pdb.set_trace() This pops you into a python prompt with access to all of the current variables. 这会将您弹出到可访问所有当前变量的python提示符下。 To see all the defined variables call locals() , though this will print out a ton of junk along with it. 要查看所有定义的变量,请调用locals() ,尽管这样会打印出大量垃圾。 In the template you can display a "you can't access this page" message if the user isn't logged in. 如果用户未登录,则在模板中可以显示 “您无法访问此页面”消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM