How to differentiate the first time registered and regular logged in user in django

◇◆丶佛笑我妖孽 提交于 2019-12-31 03:04:07

问题


I am using django allauth for all my signin, signup and logout functionality and working fine.

Now i have a functionality that,

1.when a user is registered and logged in for the first time, i need to redirect him to a success page/verification page(/success/)

2.When a user who is already registered is logged in, he should be redirected to /dashboard/

As of now i am redirected the user(first time registered and already registered) to /dashboard/ by a setting called LOGIN_REDIRECT_URL in settings.py

LOGIN_REDIRECT_URL = /dashboard/

Also i have observed that there is field/attribute called last_login for user object from which we can use to find the last login for the user, whether it will be helpful ?

model.py

from django.contrib.auth.models import User

class Profile(models.Model):
    business_name = models.CharField(max_length = 45, null = False, blank = False)
    user = models.ForeignKey(User, unique = True)
    work_field = models.CharField(max_length = 45, null = False, blank = False)
    image = models.ImageField(upload_to = '/images/', null = True, blank = True, max_length = 250)
    image.allow_tags = True
    url = models.URLField(max_length = 255, null = True, blank = True)

回答1:


I think you can check your last_login field (if you are sure a user exists).

Try one of the next:

profile.user.last_login == profile.user.date_joined: # this may be True if a user logins for a first time

OR

profile.user.last_login == None:

I don't know the exactly value for the field but you can experiment and find the rule for first-time registered users.




回答2:


Create a custom view using a authenticate(), than when you get the user object use the instance method .last_login and it will return either null or true so make a variable with a string to whatever you'll do if it's reverse or etc. Than use login() to login the user and return the variable that was decided by the user object method last_login. You would also have to find a template and define a url on yp ur urlconf. You can find many templates creating a custom form for your view



来源:https://stackoverflow.com/questions/20126536/how-to-differentiate-the-first-time-registered-and-regular-logged-in-user-in-dja

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!