Django timezone.now()保持不变

问题描述 投票:0回答:1

我正在寻找设置token authentification以便以安全的方式下载服务器文件并在到期时间后添加。

要求 :

  • Django 1.11
  • Ubuntu 18.04
  • 数据库PostgreSQL

流程:

用户填写一个表格,其中包含一些信息CustomerForm(email, ...),他必须选择一个或多个带复选框的文档。

提交表单时,将使用生成的令牌发送电子邮件。此令牌有一个到期延迟(在第一个1分钟,以进行一些测试)。

问题 :

当用户点击我的链接时,它会显示token是否在列表中,datetime.now()expiration_delay

但是,如果我再次点击链接(可能是第一个之后的30秒),datetime.now()仍然是第一个。它应该改变。

我怀疑cookie是为了保持价值,但我不知道这是不是正确的方法。

我的文件 :

我在视图中有一个类,它允许填写表单,生成令牌并发送电子邮件。

然后,我有这个新类,它可以将token与数据库进行比较,并将expiration_timenow()进行比较。

class TokenDownloadView(TemplateView):
    template_name = 'app/token.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['token'] = self.kwargs['token']
        token = context['token']
        print(token)
        download = Download.objects.get(token__iexact=token)

        if download and download.expiration_date > now:
            print("token valide jusqu'à : " + str(download.expiration_date))
            print("il est actuellement : " + str(now))
            print(' ==> Token existe et valide <==')

        if download and download.expiration_date < now:
            print("token valide jusqu'à : " + str(download.expiration_date))
            print("il est actuellement : " + str(now))
            print('==> Token existe mais a expiré <==')

        return context

这就是我在终端中获得的内容,以显示我所说的内容:

d0ce9328a53032d4484cccff4c0bdd92ad701567
token valide jusqu'à : 2018-09-12 07:46:30.082915+00:00
il est actuellement : 2018-09-12 07:45:30.082915+00:00
 ==> Token existe et valide <==
[12/Sep/2018 09:45:42] "GET /crud/download/token/d0ce9328a53032d4484cccff4c0bdd92ad701567/ HTTP/1.1" 200 7447
[12/Sep/2018 09:45:42] "GET /static/css/common-8073709e.css HTTP/1.1" 404 1682
d0ce9328a53032d4484cccff4c0bdd92ad701567
token valide jusqu'à : 2018-09-12 07:46:30.082915+00:00
il est actuellement : 2018-09-12 07:45:30.082915+00:00
 ==> Token existe et valide <==
[12/Sep/2018 09:46:10] "GET /crud/download/token/d0ce9328a53032d4484cccff4c0bdd92ad701567/ HTTP/1.1" 200 7447
[12/Sep/2018 09:46:10] "GET /static/css/common-8073709e.css HTTP/1.1" 404 1682
d0ce9328a53032d4484cccff4c0bdd92ad701567
token valide jusqu'à : 2018-09-12 07:46:30.082915+00:00
il est actuellement : 2018-09-12 07:45:30.082915+00:00
 ==> Token existe et valide <==
[12/Sep/2018 09:46:30] "GET /crud/download/token/d0ce9328a53032d4484cccff4c0bdd92ad701567/ HTTP/1.1" 200 7447
[12/Sep/2018 09:46:30] "GET /static/css/common-8073709e.css HTTP/1.1" 404 1682
d0ce9328a53032d4484cccff4c0bdd92ad701567
token valide jusqu'à : 2018-09-12 07:46:30.082915+00:00
il est actuellement : 2018-09-12 07:45:30.082915+00:00
 ==> Token existe et valide <==
[12/Sep/2018 09:46:41] "GET /crud/download/token/d0ce9328a53032d4484cccff4c0bdd92ad701567/ HTTP/1.1" 200 7447
[12/Sep/2018 09:46:41] "GET /static/css/common-8073709e.css HTTP/1.1" 404 1682

你有什么主意吗 ?

python django datetime token
1个回答
2
投票

首次导入模块时,将执行类或模块级别的任何代码,因此在该点设置在该代码中设置的任何值。如果需要在每个请求上更新值,则需要在方法中设置它们。在此代码中,将now的定义移动到get_context_data方法中将会执行您想要的操作。

© www.soinside.com 2019 - 2024. All rights reserved.