English 中文(简体)
Django 用户在页面更新时登录
原标题:Django user logged out when the page refreshes
It s really confusing me and I don t understand why. I have database backed sessions (with mongoengine) but it seems like when I visit a page then refresh it the user is no longer logged in. Essentially request.user.is_authenticated() is False after the page reset Anyone have any ideas? In my settings I have: SESSION_ENGINE = mongoengine.django.sessions MIDDLEWARE_CLASSES = ( ... django.contrib.sessions.middleware.SessionMiddleware , ... On the debug page I notice a few things: SESSION_COOKIE_DOMAIN None SESSION_EXPIRE_AT_BROWSER_CLOSE False SESSION_COOKIE_AGE 1209600 Could the absent cookie domain be a clue as towards what the problem is?
问题回答
I ran into the same problem and in the solution browser they wrote to me that samesite= None and I can t save cookies. Try to remove samesite = None
The issue you re experiencing could indeed be related to the SESSION_COOKIE_DOMAIN setting or other session-related configurations. You should check a few things: SESSION_COOKIE_DOMAIN: If this setting is None, the session cookie is only valid for the domain that served it. If you access your site from multiple domains (e.g., example.com and www.example.com), this could cause issues. Try setting SESSION_COOKIE_DOMAIN explicitly to your domain, e.g., example.com. Browser Settings: Ensure that your browser is not configured to delete cookies upon closing or refreshing. SESSION_ENGINE: Double-check that the mongoengine.django.sessions engine is correctly configured and compatible with your version of Django. Ensure that MongoDB is properly handling the sessions. SESSION_EXPIRE_AT_BROWSER_CLOSE: If this is set to True, sessions will expire when the user closes their browser. Since you have it set to False, this shouldn t be the issue, but it s good to double-check the actual behavior. Check MongoDB: Make sure that the session data is being correctly stored in MongoDB. You might want to manually check the sessions collection in your MongoDB database to see if sessions are being created and stored correctly. Middleware Order: Ensure that SessionMiddleware is placed correctly in the MIDDLEWARE_CLASSES. It should generally be one of the first middlewares so that sessions are processed early. Cookie Security: If you are accessing your site over HTTPS, make sure SESSION_COOKIE_SECURE is set to True. If you are using a non-HTTPS connection during development, ensure SESSION_COOKIE_SECURE is False. If none of these suggestions solve the issue, you might want to add some logging to the session handling parts of your code to track what happens when the page is refreshed.




相关问题
How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

Moving (very old) Zope/Plone Site to Django

I am ask to move data from a (now offline) site driven by Plone to a new Django site. These are the version informations I have: Zope Version (unreleased version, python 2.1.3 ) Python Version 2.1....

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

Flexible pagination in Django

I d like to implement pagination such that I can allow the user to choose the number of records per page such as 10, 25, 50 etc. How should I go about this? Is there an app I can add onto my project ...

is it convenient to urlencode all next parameters? - django

While writing code, it is pretty common to request a page with an appended "next" query string argument. For instance, in the following template code next points back to the page the user is on: &...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

热门标签