logout time cannot be record by using logout redirect via setting

Hello djangosss , I’m facing issue with logout time cannot generate while user click logout.

---def logout_user(request,user):
   print("hello")
   data_login = Loginusers_log(logouttimel=datetime.datetime.now()   )
   #data_logout = Loginusers_log(logouttimel=datetime.datetime.now()   )
   request.user.daily_logout = datetime.datetime.now()
   request.user.save()
   Loginusers_log.daily_logout=request.user.daily_logout
       
   data_login.save()
   
   #request.user.data_logout = datetime.datetime.now()
   #request.user.save()
   #Loginusers_log.data_logout=request.user.daily_login
       
   #data_logout.save()
   print(Loginusers_log.logouttimel)


   logout(request)

model

class Loginusers_log(models.Model):
    usernamel = models.CharField(max_length=50)
    passwordl =  models.CharField(max_length=50)
    logintimel  = models.DateTimeField(auto_now_add=False) 
    logouttimel  = models.DateTimeField()
    def __str__(self):
     return self.usernamel

thank you in advance noted login time woking perfect

First, when you’re posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of editing this post for you.)

It’s tough to tell what you’re actually trying to run here with all those lines of comments mixed in.

However, a couple things jump out at me:

Loginusers_log is the name of the model and is not the instance of the model. There’s also no field in that model named daily_logout.

Are you working with a custom user model? If not, these lines don’t do anything for you.

This is creating a new instance of Loginusers_log for you, but that’s the only field in that object you are populating.

(Side note: I really hope you’re not storing password in this log table - and definitely not in plain text.)

Master sorry I m new here and very blue in django . i think i found issue is related to my logout views , when user click logout , the redirection in setting.py redirect without pointing to my views logout , i change my logout views name and now i think i will get the logout time .

I have update my model ```
[/quote]
‘’‘’’ my model’‘’‘’
class Logoutusers_log(models.Model):
usernamel = models.CharField(max_length=50)
timeloggoff = models.DateTimeField(auto_now_add=False)
def str(self):
return self.usernamel

‘’‘’‘’

my logout function views
“”“”
def logoutUser(request):
logout(request)
print(“this me”)
Logoutusers_log.timeloggoff=datetime.datetime.now()
print(datetime.datetime.now() )
return redirect(‘user:home’)
‘’‘’’
I want to save this field Logoutusers_log.timeloggoff in order to save time on The table
Sir thank you in advance

Side note: For formatting, you need to use the backtick - ` characters, and the lines of ``` must be lines by themselves. They may not be at the beginning or end of any other lines.

How are you handling the logon logging that you’re saying is working? How is this different from that?

I have two form in one page to handle sign and registration .I don’t use signupdjango format in order to apply some style

I have issue with logout because i need to record logout time i find the solution the update field will not work with time field need to add update attribute

I’m referring to your view.

What does your login view look like that saves the login time?


:blush:

i found something magic but its not professional work the auto save false

First, please do not post images of code here. Copy / paste the code directly into the body of your post surrounded by lines of ``` as described above.

Second, I was asking for your view that handles logging in, which you stated above was working perfectly.

def logoutUser(request):
logout(request)
print(“this me”)
Logoutusers_log.timeloggoff=datetime.datetime.now()
print(datetime.datetime.now() )
return redirect(‘user:home’)
def login_user(request):
    if  request.method =='POST':
       password= request.POST.get('password')   
       username = request.POST.get('username')
       data_login = Loginusers_log(usernamel=username,passwordl=password, logintimel=datetime.datetime.now()   )
       user = authenticate(request, username=username, password=password)
if user is not None:
        login(request,user)
        request.user.daily_login = datetime.datetime.now()
        request.user.save()
        Loginusers_log.daily_login=request.user.daily_login
        
        data_login.save()
        print(data_login)
        print(Loginusers_log.daily_login,"hello")
        return redirect('user:home')
       else:
        messages.success(request,("Wrong username or password"))
        return render(request,'./authenticate/login.html')
else:
     return render(request,'./authenticate/login.html' ,{})

Looking at only the login_user method, which one specific line of code creates an instance of the Loginusers_log model?

if user is not None:
        login(request,user)
        request.user.daily_login = datetime.datetime.now()
        request.user.save()
        Loginusers_log.daily_login=request.user.daily_login

[/quote]

for login process  i use   login(request,user) 

but not have for logout  only  logout(request)

[/quote]

That is not correct. None of those lines are responsible for creating an instance of the Loginusers_log model.

Have you worked your way through the Official Django Tutorial? If you have, you might want to review the work you would have done on step 2.

If you haven’t, you should really step away from this project and work your way through the tutorial at this point in time.

You might also want to review the Python documentation on classes.

this way i cannot save and update time field on logout process

I m new on django but i will do my best to achieve this