1👍
In general it looks good. You can make it a bit more Pythonic though:
class SetGroupMiddleware(object):
def process_request(self, request):
if 'thegroup' not in request.session:
if not request.user.is_anonymous():
groups = request.user.groups.all()
if groups:
request.session['thegroup'] = str(groups[0].name)
else:
request.session['thegroup'] = None # for completeness
0👍
It looks approximately correct (not having tested it). One thing to note is that your middleware must occur after django.contrib.sessions.middleware.SessionMiddleware
in the MIDDLEWARE_CLASSES list, otherwise the session won’t have been setup for you at the time you try to reference it.
- [Django]-UserWarning: Module _mysql was already imported from /usr/lib/pymodules/python2.6/_mysql.so
- [Django]-What's the correct include path in this template?
- [Django]-Django Serializer returns JSON for parent class objects only and leave child objects as same?
- [Django]-Serving static files for development mode in Django
- [Django]-Django session lost when redirected from facebook oauth
0👍
Well, as I commented in Steve Losh’s answer , that code doesn’t work as intended.
I modified it as follows and it seems to be OK till now: –
class SetGroupMiddleware(object):
def process_request(self, request):
if not request.user.is_anonymous():
if 'thegroup' not in request.session:
groups = request.user.groups.all()
if groups:
request.session['thegroup'] = str(groups[0].name)
- [Django]-Creating a User Registration Page using MongoEngine
- [Django]-Monitoring django postgres connections
- [Django]-Django: How to keep track of a linear (yet flexible) project management workflow?
Source:stackexchange.com