django,在身份验证后重定向到ajax操作

时间:2013-04-30 14:00:00

标签: python ajax django jquery django-authentication

我在图像上有链接,如果用户登录了ajax,可以轻松地进行upvote / downvote。但是,如果用户未登录,我确保首先对用户进行身份验证。

实现在模板中完成,如下所示 -

{% if not user.is_authenticated %}
<a href="/accounts/login/">Get Started</a>
{% else %}
<a href="my-ajax-url"> Like </a>
{% endif %}

这里的问题是如果用户没有登录,我希望他先登录,然后执行ajax操作。与 next 类似的内容有助于实现。所以流程应该是,如果用户没有被记录,他将被带到登录URL,然后自动进行ajax动作。

实现它的另一种方法是实现 ajax_login_decorator ,可以here完成。使用第二种方法,流程类似于:

1. The user clicks on the action button.
2. the request goes to the server, if he is not authenticated, a response is sent to client.
3. He is made to login/signup.
4. Then once he logs in, he has to click on the action button again. 

相反,更好的方法可能是,他首先登录,类似 next 的操作可以执行ajax操作。所以问题归结为这个简单的事情 -

如何在用户通过身份验证后调用带有next个参数的ajaxy网址。

1 个答案:

答案 0 :(得分:1)

在第2步中将操作存储在(匿名)会话中,一旦登录,您从服务器端触发操作怎么样?您根本不必进行AJAX调用。

相关问题