Django: How can I check the last activity time of user if user didn’t log out?

You need to have the last_activity field in the user profile (or custom user model). This field will be updated on every request. To achieve this you need to have custom middleware: profiles/middleware.py: from django.utils import timezone from myproject.profiles.models import Profile class UpdateLastActivityMiddleware(object): def process_view(self, request, view_func, view_args, view_kwargs): assert hasattr(request, ‘user’), ‘The UpdateLastActivityMiddleware requires … Read more

Auth::user() returns null

I faced a situation where Auth::user() always returns null, it was because I was trying to get the User in a controller’s constructor. I realized that you can’t access the authenticated user in your controller’s constructor because the middleware has not run yet. As an alternative, you can define a Closure based middleware directly in … Read more

req.locals vs. res.locals vs. res.data vs. req.data vs. app.locals in Express middleware

As you mentioned, both req.locals, res.locals or even your own defined key res.userData can be used. However, when using a view engine with Express, you can set intermediate data on res.locals in your middleware, and that data will be available in your view (see this post). It is common practice to set intermediate data inside … Read more

Passing variables to the next middleware using next() in Express.js

This is what the res.locals object is for. Setting variables directly on the request object is not supported or documented. res.locals is guaranteed to hold state over the life of a request. res.locals (Note: the documentation quoted here is now outdated, check the link for the most recent version.) An object that contains response local … Read more

bodyParser is deprecated express 4

It means that using the bodyParser() constructor has been deprecated, as of 2014-06-19. app.use(bodyParser()); //Now deprecated You now need to call the methods separately app.use(bodyParser.urlencoded()); app.use(bodyParser.json()); And so on. If you’re still getting a warning with urlencoded you need to use app.use(bodyParser.urlencoded({ extended: true })); The extended config object key now needs to be explicitly … Read more

How can I enable CORS on Django REST Framework

The link you referenced in your question recommends using django-cors-headers, whose documentation says to install the library python -m pip install django-cors-headers and then add it to your installed apps: INSTALLED_APPS = ( … ‘corsheaders’, … ) You will also need to add a middleware class to listen in on responses: MIDDLEWARE = [ …, … Read more