广告区

广告区


本站消息

站长简介/公众号

关注本站官方公众号:程序员总部,领取三大福利!
福利一:python和前端辅导
福利二:进程序员交流微信群,专属于程序员的圈子
福利三:领取全套零基础视频教程(python,java,前端,php)

  价值13000svip视频教程,python大神匠心打造,零基础python开发工程师视频教程全套,基础+进阶+项目实战,包含课件和源码

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

查看不更新当前日期(Django)

发布于2022-05-21 12:13     阅读(265)     评论(0)     点赞(8)     收藏(1)



它是在视图中调用 get_week 实现的。'now',直接打印为模板,正常显示,但是通过模块输出的get_week的结果,输出的是1周前的值。如果你修复了类似的bug,请帮助参考,下面的源代码是一个生成周报的函数。



#: 금주 보고서는 read/modify
@login_required
def weekly_report_view(request):
    context = {
        'site_title': '취약점 Dashboard',
        'site_header': '취약점 Dashboard',
        'user': request.user,
        'has_permission': True,
        'now': datetime.datetime.now()
    }

    this_week = False
    week_num = request.GET.get('week_num')
    if week_num is None:
        start_date, end_date, week_num = get_week()
    stats = Statistics2020.objects.filter(week_num=week_num)
    if len(stats) == 0:
        return redirect('/dashboard/stat/weekly/create')
    stat = stats[0]
    if stat.end_date >= datetime.date.today():
        this_week = True
        stat_date_update(stat.week_num, stat.start_date, stat.end_date)
    else:
        pass
    context['stat'] = Statistics2020.objects.get(week_num=week_num)
    context['raw'] = json.loads(stat.raw)
    context['this_week'] = this_week
    return render(request, template_name='dashboard/weekly_report.html', context=context)


def get_week(today=datetime.datetime.now().date()):
    shift = BASE_WEEK_START - 1
    print(today)
    week_number = today.isoweekday() - shift
    if week_number <= 0:
        week_number += 7
    startdate = today - datetime.timedelta(days=week_number-1)
    enddate = startdate + datetime.timedelta(days=6)
    #print('%s~%s'% (startdate, enddate))
    return startdate, enddate, int(startdate.isocalendar()[1])

解决方案




Python 3.6.8 (tags/v3.6.8:3c6b436a57, Dec 24 2018, 00:16:47) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> import datetime
>>> def hello(now=datetime.datetime.now()):
    print(now)

>>> hello()
2020-07-30 13:25:34.731001
>>> hello()
2020-07-30 13:25:34.731001
>>> hello()
2020-07-30 13:25:34.731001
>>> hello(datetime.datetime.now())
2020-07-30 13:25:53.446748
>>> hello(datetime.datetime.now())
2020-07-30 13:25:59.613445
>>> 






所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/1533939/787dcd5c6859bfa901ee/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

8 0
收藏该文
已收藏

评论内容:(最多支持255个字符)