【问题标题】:Python/Django not refreshing current date on serverPython/Django 不刷新服务器上的当前日期
【发布时间】:2023-02-26 18:06:41
【问题描述】:

我在我的服务器上使用日历。应该标记当前日期,但不会刷新它。结果,标记的日期是服务器重新启动的日期。你有解决这个问题的方法吗?我希望它标记正确的日期,而不是服务器发生的日期。感谢帮助。

更新 这是我使用的代码

这是整个视图功能

    today = datetime.datetime.today()

    #pulling models from database
    employee = Employee.objects.filter(barberName=Barber)
    reservations = Appointments.objects.all()

    apptDict = {}
    timeList = []

    #make list of all possible appointments for the day
    for x in employee:
        startTime = "28/02/22 " + str(x.barberStartTime)
        endTime = "28/02/22 " + str(x.barberEndTime)
    
    startTimeObj = datetime.datetime.strptime(startTime, '%d/%m/%y %H:%M:%S')
    endTimeObj = datetime.datetime.strptime(endTime, '%d/%m/%y %H:%M:%S')

    while startTimeObj < endTimeObj:
        justTimeList = str(startTimeObj).split(" ")[1].split(":")
        timeList.append(justTimeList[0] +  ":" + justTimeList[1])
        startTimeObj += datetime.timedelta(hours=0.5)
    apptDict["possibleTimes"] = timeList

    #make dictionary of already made appointments in form "date": ["time1", "time2", "timeN"]
    for reservation in reservations:
        time = str(reservation.time).split(":")
        timeStr = time[0] + ":" + time[1]
        if str(reservation.date) not in apptDict:
            apptDict[str(reservation.date)] = []
        apptDict[str(reservation.date)].append(str(timeStr))
        if reservation.date < today.date():
            apptDict[str(reservation.date)] = ["Over"]
    
    #write to json file
    with open("reservations/static/reservations/scripts/appointments.json", "w") as outfile:
        json_object = json.dumps(apptDict, indent=4)
        outfile.write(json_object)

    context = {
        "place": Place,
        "barber": Barber,
        "service": Service,
        "calendar": newCalendar(datetime.datetime.today().month, datetime.datetime.today().year, Barber),
        "employee": employee,
        "availableTimes": timeList,
        "apptDict": apptDict,
        "availTimes": timeList
    }

    if request.method == "POST":
        postCopy = request.POST.copy()
        postCopy['time'] = datetime.datetime.strptime('15/05/22 ' + postCopy['time'], '%d/%m/%y %H:%M').time()
        postCopy['phone'] = str(postCopy['phone'])
        request.POST = postCopy
        form = AppointmentForm(request.POST or NONE)
        if form.is_valid():
            form.save()
            return render(request, 'reservations/reserved.html', context)
    return render(request, 'reservations/reservationTime.html', context)

这是日历功能的一部分。它应该在视觉上标记前几天

for x in range(1, int(today.day)):
   cal = cal.replace(">" + str(x) + "<", 'style="color: #808080;">' + str(x) + "<") #marking previous days in month

【问题讨论】:

  • 您如何在日历中获取当前日期?
  • datetime.today() 它在函数内部使用
  • 您介意发布相关代码吗?
  • 我更新了一下,希望对你有帮助
  • 您没有提供足够的代码来解决问题。但是,根据您的描述,创建日历的函数 100% 是在导入时运行的,而您需要在视图函数中运行它。

标签: python django server


【解决方案1】:

你弄清楚问题了吗?请分享修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多