简体   繁体   English

无需重新加载页面的Django身份验证

[英]Django authentication without reloading page

I'm rather new to front-end web programming and am trying to mimic the iCloud.com login page. 我是前端Web编程的新手,并且正在尝试模仿iCloud.com登录页面。 I have the whole layout. 我有整个布局。 I have functions for the transition on successful login as well as the shake on unsuccessful login but I can't for the life of me figure out how to check user authentication with django without changing pages, and then activate these functions accordingly. 我具有成功登录时的过渡功能以及失败登录时的抖动功能,但是我一辈子都无法弄清楚如何在不更改页面的情况下使用django检查用户身份验证,然后相应地激活这些功能。 I can't get ajax or anything to work. 我无法获得Ajax或任何可用的工具。

Anyone have any idea how this is done that can point me in the right direction. 任何人都知道如何做到这一点,可以为我指明正确的方向。 Right now I'm pretty sure what I'm trying to do is submit an ajax form. 现在,我非常确定我要执行的操作是提交ajax表单。

User authentication is nothing special. 用户身份验证没有什么特别的。 And it works the same for both ajax and non ajax views. 对于ajax和非ajax视图,它的工作原理相同。

I suggest you implement authentication without ajax first. 我建议您先实现无需ajax的身份验证。 Confirm it is working and then go for the ajax. 确认它正在工作,然后使用ajax。

But the simple example would be: 但是简单的例子是:

1) Add login url to urls 1)在网址中添加登录网址

from django.contrib.auth import views as auth_views
....
url(r'^authenticate/$',
        auth_views.login,
        {'template_name': 'yourapp/login.html'},
        name='auth_login'),

As you can see you can give template as keyword argument to that view. 如您所见,您可以将模板作为关键字参数提供给该视图。 So you can use djangos own auth view for authenticating with your template 因此,您可以使用djangos自己的身份验证视图对模板进行身份验证

2) Add link to your page which, after clicking on it. 2)单击页面后,将链接添加到您的页面。 Does get request and reads the template and puts it into some kind of modal. 获取请求并读取模板并将其放入某种模式。 This is relatively easy and many jQuery modal plugins support this feature. 这相对容易,许多jQuery模态插件都支持此功能。 I've not user jQuery for years now so finding solid example is probably just as easy for you as it is for me 我已经好几年没有使用jQuery了,所以找到可靠的示例可能对您和对我一样容易

3) Modal window will present your own login.html. 3)模态窗口将显示您自己的login.html。 In that file you post the form to the same url where it came from. 在该文件中,将表单发布到它来自的相同URL。 You do it by setting form action attribute to {{ request.get_full_path }} 您可以通过将表单操作属性设置为{{ request.get_full_path }}

4) You also have to implement some js code which will reset form on submit to display login errors. 4)您还必须实现一些js代码,这些代码将在提交时重置表单以显示登录错误。

5) After successful submit comes probably the hardest part as you have to do most coding here. 5)成功提交之后可能是最困难的部分,因为您必须在此处进行大多数编码。 After logging in using django view, you get redirected if you do not use ajax. 使用django视图登录后,如果不使用ajax,将被重定向。 You can handle it two ways - expect it and use returned redirect as as hint that you can display "login success" message or make another ajax request to your server to get "login successful" template and refresh the areas of your site which need to display the login (logged in or not) status. 您可以通过两种方式处理它-预期并使用返回的重定向作为提示,您可以显示“登录成功”消息或向服务器发出另一个ajax请求,以获取“登录成功”模板并刷新需要显示登录(已登录或未登录)状态。

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

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