[Python] How to calculate the time variable and comapre a time data with the current time by...

Discussão em 'Python' iniciado por Stack, Junho 10, 2025.

  1. Stack

    Stack Membro Participativo

    For example, I have a model in models.py which looks like this:

    class BlackList(models.Model):
    username = models.CharField(max_length=1000)
    flag1 = models.BooleanField(default=False)
    trytologintime = models.DateTimeField(auto_now_add=True, null=True)


    And in the views.py, what I try to do is something like this:

    ......
    username = request.POST.get('username') # Get username input first
    password = request.POST.get('password')
    user = authenticate(request, username=username, password=password)
    black_list_user = BlackList.objects.get(username=username)
    # timenow = get time for now ( I do not know how to get it foe now )
    passtime = timenow-black_list_user.trytologintime (Also do not know how to do the subtraction for time variable)
    if passtime > 24 hr (How to compare the time)
    black_list_user.flag1 = True
    black_list_user.save()
    ......


    My questions are:

    1. How to get the current time?
    2. How to do subtraction and compare two time variables?

    Continue reading...

Compartilhe esta Página