Django created_at__gt = self.request.user.last_login仅对已登录的用户有效。 [英] Django created_at__gt=self.request.user.last_login workinly only on users already signed in.

查看:75
本文介绍了Django created_at__gt = self.request.user.last_login仅对已登录的用户有效。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简介:我有3种型号的用户:用户帖子。用户可以发表帖子,但是每个帖子必须属于一个组。用户必须从现有组中选择其帖子。用户无法添加,删除或更新组的用户。

Intro: I have a 3 models user, post, group. User is able to make posts however each post has to belong to a group. Users have to choose from the existing groups for their posts. Users cannot add, delete, update group's.

此外:

用户可以在何时以及何时成为组的成员。他们单击某个组。他们会看到该组中的所有帖子。

Users can become a member of groups and when they click on a certain group. They see all the posts in that group.

我想要的当用户进入主页时,他们看到的是自上次登录后添加的帖子

What I want When Users come on the home page they see posts that were added since the last time they logged in

我的模型

class Post(models.Model):
    user = models.ForeignKey(User, related_name='posts')
    group = models.ForeignKey(Group, related_name='posts')
    title = models.CharField(max_length=250, unique=True)
    message = models.TextField()
    created_at = models.DateTimeField(auto_now=True)

我的视图

class Homepage(TemplateView):
    template_name = 'home.html'

    def get_context_data(self, **kwargs):
        context = super(Homepage, self).get_context_data(**kwargs)  
        if self.request.user.is_authenticated():
            context['object_list'] = Group.objects.filter(members=self.request.user)

            #What am I doing wrong in the below code
            new_posts = Post.objects.filter(created_at__gt=self.request.user.last_login).count()
            context['new_posts'] = new_posts
         else:
              context['object_list'] = Group.objects.all()
        return context

在我的模板中,

<div class="list-group">
   {% for group in object_list %}        
       {% if not new_posts %}
               {{group.post.count}}
        {% else %}
                {{new_posts}}
        {% endif %}
    {% endfor %}
</div>




问题:仅以下用户已登录并刷新其页面,请参见新帖子示例: 4个新 3个新等。 。如果用户在组中创建新帖子后重新登录。他没有看到 4个新 3个新。相反,它只向他显示该组中的职位数量。自从他登录以来没有新帖子。为什么会这样?

The Issue: Only the users who are already signed in and refresh their page see the new posts example:4 new, 3new etc... If a user signs in fresh after a new posts are created in a group. He does not see the 4 new, 3new . Instead it shows him just the number of posts in the group. not the new posts since he logged in. Why is this happening?


推荐答案

嗯,它的正常行为是因为 last_login 存储用户上次登录的日期时间。当用户全新登录到系统时,它存储当前时间(timezone.now)。因此,如果在登录之前创建了帖子,则该帖子不会出现在 new_posts 中。因此,如果要存储以前的登录/注销时间,则需要将其存储在其他位置(或创建新的模型字段)。您可以使用用户进行尝试注销信号

Well, its normal behavior because last_login stores datetime at which the user last logged in. When the user fresh logs in into the system, it stores the current time (timezone.now). So if a post is created before he is logged in, then it will not appear into the new_posts. So if you want to store the previous login/logout time, then you need to store it somewhere else(or make a new model field). You can try like this using user logged out signal:

from django.contrib.auth.signals import user_logged_out


def do_stuff(sender, user, request, **kwargs):
    user.logout_time = timezone.now()
    user.save()

user_logged_out.connect(do_stuff)  # Hook an a method to user_logged_out signal to update logout time

这样的视图:

last_post = Post.objects.last()
if last_post.created_at < request.user.last_login:
   new_posts = Post.objects.filter(created_at__gt=self.request.user.logout_time).count()
   context['new_posts'] = new_posts



更新



在此示例中,必须具有一个字段存储注销时间。如果您有 CustomUser ,则可以直接在CustomUser模型中创建它。否则,您可以这样添加它:

Update

In this example, it is required to have a field to store logout time. If you have a CustomUser then you can directly create it in the CustomUser Model. Else, you can add it like this:

class Profile(models.Model):
    user = models.OneToOneField(User)
    logout_time = models.DateTimeField(default=timezone.now)

并存储这样的注销时间:

and store logout time like this:

user.profile.logout_time = timezone.now()

并过滤新帖子,如下所示:

and filter New Posts Like this:

new_posts = Post.objects.filter(created_at__gt=self.request.user.profile.logout_time)

这篇关于Django created_at__gt = self.request.user.last_login仅对已登录的用户有效。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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