有没有办法添加自定义代码到login_required [英] Is there a way to add custom code to login_required

查看:1601
本文介绍了有没有办法添加自定义代码到login_required的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经自己制作了 LoginRequiredMixin ,如下所示:

I've made my own LoginRequiredMixin like this:

class LoginRequiredMixin(object):
    @classmethod
    def as_view(cls, **initkwargs):
        view = super(LoginRequiredMixin, cls).as_view(**initkwargs)
        # (!!) multilangue = reverse_lazy, PAS reverse
        return login_required(view, login_url=reverse_lazy('my_home_login'))

到目前为止,当您创建新视图时,一切都可以正常工作:

So far so good, everything works fine when you create new views like this:

class EditView(LoginRequiredMixin, generic.UpdateView):
    model = Personne
    template_name = 'my_home/profile/base.html'
    form_class = ProfileForm
    success_url = reverse_lazy('my_home_profile_edit')

等等。

现在,我的客户端要求我实施一个选项:如果用户想要删除他的帐户,我必须将其标记为不活动,发送一封重新启动的电子邮件n链接有效期为15天,如果用户在没有点击重新启动链接的15天内尝试登录,我应该显示一条消息,说明您的帐户已被禁用,请点击我们发送的链接

Now, my client asked me to implement an option: if the user wants to delete his account, I have to mark it as "inactive", sends an email with a re-activation link that is valid for 15 days and if the user tries to login in within those 15 days without having clicked on the re-activation link, I should display a message saying "your account has been disabled, please click on the link we sent".

所以我想实现用户登录后的不活动帐户,并显示您的帐户被禁用消息。因为我想要显示任何URL(我的个人资料,我的旅行,我的好友,我的消息或其他),我应该在$ code> LoginRequiredMixin 类中。问题是:我不知道该怎么做例如,如果有一个,我需要覆盖所有 template_name ,并禁止每个操作显示消息。

So I want to implement the "inactive" account after the user is logged in, and display the "your account is disabled" message. Because I want to display it whatever the URL is (my profile, my travels, my buddies, my messages or whatever), I should do it in the LoginRequiredMixin class. The problem is: I dont know how to do it. For example, I need to override all template_name if there's one, and forbid every action but display the message.

如何做?

推荐答案

看起来像请求/响应中间件是更好的地方来检查,不是吗?在那里你可以检查用户是否已经登录,如果它被禁用。并且在那里进行大量的数据操作。

Looks like request/response middleware is better place to check this, isn't it? There you can check if user is already logged in and if it is disabled. And make a lot of data manipulations there.

至于我,实际上并不需要登录,因为用户已经登录了,因为最好放置你登录中间件。例如,如果您使用默认用户模型选项 is_active ,则可以检查它(或任何其他标志),并将用户重定向到每个重新启动的某个模板,在此请求激活其帐户。

As for me, it is not actually login required, since user is already logged in. It loooks like it is better to place your login into middleware. For example, if you use default Users model option is_active, you can check it (or any other flag) and redirect user to some template for each reaquest, where you asking it to activate its account.

例如,您可以让这样的中间件: https://djangosnippets.org/snippets/510/
这里是具有非常干净的请求处理模式的中间件的django文档: https://docs.djangoproject.com/en/1.9/topics/http/middleware/
其他教程: http://www.webforefront.com/django/middlewaredjango.html 看起来它对每个方法实际上都有非常干净的评论。
以下是如何设置自定义中间件:如何设置自定义中间件在django

For example you can make you middleware like this: https://djangosnippets.org/snippets/510/ Here is the django documentation for middlewares with very clean schema of request handling: https://docs.djangoproject.com/en/1.9/topics/http/middleware/ Other tutorial: http://www.webforefront.com/django/middlewaredjango.html Looks like it has very clean comments about what each method actually does. Here is how to setup your custom middleware: how to setup custom middleware in django

这篇关于有没有办法添加自定义代码到login_required的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆