0

when a user signs up, they get redirected to the homepage. If it is their first time logging in, I want to display a div, however if it is not their first time, I do not want this div to appear. Currently, I am relying on date_joined andlast_login to do this check.

However this only checks dates and not the time. Therefore the div will still appear until the date has changed. I only want the div displayed once. This is when the user first logs in.

Here is my code so far: views.py:

def home(request):
    context={}
    user = request.user
    if user.last_login.date() == user.date_joined.date():
        context['isFirstTime'] = 'isFirstTime'
    else:
        pass
    return render(request, 'home.html', context)

template:

   {% if isFirstTime %}
        <div style="width: 300px; height: 300px; background-color: green;">

        </div>
    {% endif %}

Does anybody know how I can alter this so it works with the current time and not the current date. This way the div is only displayed when a user first logs in. Anybody know a solution? Thank you. Also date_joined and last_login are datetime objects stored in the database.

NodeReact020
  • 486
  • 5
  • 23
  • Both `last_login` and `date_joined` are DateTime values – yorodm Mar 11 '20 at 19:26
  • @yorodm Yes, but how do I change the code so it does what I want. – NodeReact020 Mar 11 '20 at 19:32
  • @yorodm Only display the div if `last_login` is equal to `date_joined ` in terms of date and time. The current code only works for the date. How do I change it so it checks the current time for both these variables? What would the code look like? – NodeReact020 Mar 11 '20 at 19:34
  • Wouldn't it be easier if you create a `UserProfile ` with an attribute `is_first_login` to track this? Default it = `True` and set it to `False` after their first login? – Yellowduck Mar 11 '20 at 22:02
  • @Yellowduck Once set to False after first log in, I would want to display a div. How would I set this variable to True, therefore the div disappears after the first login? – NodeReact020 Mar 11 '20 at 22:21

2 Answers2

2

Instead of using last_login and date_joined, consider creating a UserProfile model with an attribute is_first_login, you can read more about UserProfile here .

In your models.py:

class UserProfile(models.Model):  
    user = models.OneToOneField(User, related_name='profile')
    is_first_login = models.BooleanField(default=True)  

In your view:

def home(request):
    context={}
    user = request.user
    if user.profile.is_first_login:
        context['isFirstTime'] = 'isFirstTime'
        user.profile.is_first_login = False
        user.profile.is_first_login.save()
    else:
        pass
    return render(request, 'home.html', context)
Yellowduck
  • 482
  • 2
  • 4
1
if (user.last_login - user.date_joined).total_seconds() < 5:
    ... # do your code
    user.last_login = datetime.datetime.now()
    user.save()

Both last_login and date_joined are DateTime instances so you need to compare them directly. But I recommend you to add some delta, like 5 seconds in example before to be sure

Andrey Nelubin
  • 3,084
  • 1
  • 20
  • 25
  • Ok so the div does appear. However how do I make it disappear? – NodeReact020 Mar 11 '20 at 20:09
  • As I only want to display the div when the use **first** logs in. – NodeReact020 Mar 11 '20 at 20:10
  • @PythonNinja007 well the easiest way is to update last_login. I updated the answer – Andrey Nelubin Mar 11 '20 at 20:14
  • One question though. I do not understand the code. I do not understand what the part within the if statement brackets does, or the bit inside the if statement itself. And also what does the value 5 do? Can you explain what all these parts of the code do? Thanks. – NodeReact020 Mar 11 '20 at 20:19
  • @PythonNinja007 the value in brackets is the result of datetime subtraction. It's a ```datetime.timedelta``` instance which has ```total_seconds``` method which returns a number of seconds between two dates. 5 here is threshold. So I say here "This two dates differ by 5 second or less" – Andrey Nelubin Mar 11 '20 at 20:26
  • Ah ok but I am still confused about what happens **inside** the if statement can you explain this further. I am also still a tiny bit confused about what the value **5** represents. Thank you. – NodeReact020 Mar 11 '20 at 20:53
  • @PythonNinja007 I've answered you earlier. 5 - is number of seconds. Inside of a statement I just updating user.last_login time so last_login will always be bigger than date_joined (more than 5 seconds) – Andrey Nelubin Mar 12 '20 at 05:26