返回渲染发送到正确的位置,但没有数据,直到页面重新加载?

时间:2018-08-23 15:25:03

标签: django

我有一个供员工签到/签出的时钟应用程序,该应用程序可以正常工作,但是当您位于用户活动页面上时,请在签入或签出表格后将其寄回给您到user-activity.html页,但是表中没有数据加载,也没有从视图中加载数据。刷新页面后,数据仍然存在,但我不确定为什么会这样。我将附加2张图片,其中一张在按下“签入/签出”按钮之前,另一张在

之后

这是我的看法。感谢您提供的帮助

class ActivityView(LoginRequiredMixin, View):

    def get(self, request, *args, **kwargs):
        context = {}
        toggle = UserActivity.objects.current(request.user)
        user_data = UserActivity.objects.filter(user=request.user).order_by('-timestamp')
        current_users = UserActivity.objects.order_by('user', '-timestamp').distinct('user')

        context['toggle'] = toggle
        context['user_data'] = user_data
        context['current_users'] = current_users
        return render(request, "administration/timesheets/user-activity.html", context)

    def post(self, request, *args, **kwargs):
        context = {}
        toggle = UserActivity.objects.toggle(request.user)
        context['toggle'] = toggle
        return render(request, "administration/timesheets/user-activity.html", context)

Before checkin/checkout button is pressed

After checkin/checkout button is pressed

编辑

这也是我的模特...

USER_ACTIVITY_CHOICES = (
('checkin', 'Check In'),
('checkout', 'Check Out'),
)

class UserActivityManager(models.Manager):

    def current(self, user):
        current_obj = self.get_queryset().filter(user=user).order_by('-timestamp').first()
        return current_obj

    def toggle(self, user):
        last_item = self.current(user)
        activity = "checkin"
        if last_item is not None:
            if last_item.timestamp <= tz.localize(datetime.datetime.now()):
                pass
            if last_item.activity == "checkin":
                activity = "checkout"
        obj = self.model(
                user=user,
                activity=activity
        )
        obj.save()
        return obj

class UserActivity(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    activity = models.CharField(max_length=120, default='checkin', choices=USER_ACTIVITY_CHOICES)
    timestamp = models.DateTimeField(auto_now_add=True)
    time_delta = models.DecimalField(decimal_places=2, max_digits=4, default='0.00', blank=True, null=True)
    status = models.CharField(max_length=60, null=True, blank=True)
    description = models.TextField(blank=True, null=True)

    objects = UserActivityManager()

    def __unicode__(self):
        return str(self.activity)

    def __str__(self):
        return str(self.activity)

    class Meta:
        verbose_name = 'User Activity'
        verbose_name_plural = "User Activities"

    def next_activity(self):
        next = "Check in"
        if self.activity == "checkin":
            next = "Check out"
        return next

    @property
    def current(self):
        current = 'Checked Out'
        if self.activity == 'checkin':
            current = "Checked in"
        return current

    def clean(self, *args, **kwargs):
        if self.user:
            user_activities = UserActivity.objects.exclude(
                                id=self.id
                            ).filter(
                                user = self.user
                            ).order_by('-timestamp')
            if user_activities.exists():
                recent_ = user_activities.first()
                if self.activity == recent_.activity:
                    message = "%s is not a valid activity for this user" %(self.get_activity_display())
                    raise ValidationError(message)
            else:
                if self.activity != "checkin":
                    message = "%s is not a valid activity for this user" %(self.get_activity_display())
                    raise ValidationError(message)

        return super(UserActivity, self).clean(*args, **kwargs)

1 个答案:

答案 0 :(得分:0)

在您的帖子功能中,我认为您输入有误:

toggle = UserActivity.objects.toggle(request.user)

我认为,您需要:

toggle = UserActivity.objects.current(request.user)