Some extra info. I printed the _thread_locals variable in the
get_current_user() method and in the process_request() method. They
are different:

middleware: <thread._local object at 0x84e5e60>
get_current_user: <thread._local object at 0xb6f70d70>


Here's the threadlocals.py:
import logging

try:
    from threading import local
except ImportError:
    from django.utils._threading_local import local

_thread_locals = local()
def get_current_user():
    print "get_current_user: %s " % _thread_locals
    return getattr(_thread_locals, 'user', None)

class ThreadLocals(object):
    """Middleware that gets various objects from the
    request object and saves them in thread local storage."""
    def process_request(self, request):
        print "middleware: %s" % _thread_locals
        _thread_locals.user = getattr(request, 'user', None)


and this is how I use it (the TemplateManager is used as the object
manager of a model class - objects=TemplateManager() - and I want to
use the user for filtering):

from cms.middleware import threadlocals

class TemplateManager(models.Manager):
    def get_query_set(self):
        threadlocals.get_current_user()
        return super(TemplateManager, self).get_query_set()


Looks pretty much the same as the cookbook example I think...

On 20 apr, 16:45, Hilbert Schraal <[EMAIL PROTECTED]> wrote:
> Hi All,
>
> I've followed the CookBook (http://code.djangoproject.com/wiki/
> CookBookThreadlocalsAndUser) to have the user available in my model. I
> have added the ThreadLocals class to my code and added it to the
> middleware class setting. It gets called en sets values that look
> valid.
>
> However, when I call the get_current_user() method, it always returns
> None. I tried this using 'manage.py runserver' and using mod_python.
>
> I use Django 0.96.1 on Ubuntu Hardy (Python 2.5.2) and Debian (Python
> 2.4.4). Here's my settings.py (I replaced the project name with XXX).
>
> Anyone got a clue?
> Thanks,
> Hilbert
>
> # Django settings for XXX project.
> import logging
>
> #----------------------------------------------------------------------------------------------
> #
> # Below are the settings that most likely change per
> environment                               #
> #----------------------------------------------------------------------------------------------
> #
>
> #DEBUG needs to be 'False' on environments other than development
> # NOTE: with DEBUG = True, some settings are overridden ot the bottom
> of this file.
> DEBUG = True
> TEMPLATE_DEBUG = DEBUG
>
> # logging configuratie
> LOG_LEVEL = logging.DEBUG
> LOG_FILE = '/tmp/XXX.log'
>
> DATABASE_ENGINE = 'mysql'           # 'postgresql_psycopg2',
> 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
> DATABASE_NAME = XXX             # Or path to database file if using
> sqlite3.
> DATABASE_USER = XXX             # Not used with sqlite3.
> DATABASE_PASSWORD = XXX         # Not used with sqlite3.
> DATABASE_HOST = ''             # Set to empty string for localhost.
> Not used with sqlite3.
> DATABASE_PORT = ''             # Set to empty string for default. Not
> used with sqlite3.
>
> # location of the cache. Needs to be an absolute path
> CACHE_BACKEND = 'file:///tmp/django_cache'
>
> # Absolute path to the directory that holds media.
> # Example: "/home/media/media.lawrence.com/"
> MEDIA_ROOT = '/home/schraal/XXX-media/'
>
> TEMPLATE_DIRS = (
>     # Put strings here, like "/home/html/django_templates" or "C:/www/
> django/templates".
>     # Always use forward slashes, even on Windows.
>     # Don't forget to use absolute paths, not relative paths.
>     "/home/schraal/XXX/cms/templates",
> )
>
> #----------------------------------------------------------------------------------------------
> #
> # DO NOT CHANGE THE SETTINGS BELOW HERE, UNLESS YOU KNOW WHAT YOU'RE
> DOING                     #
> #----------------------------------------------------------------------------------------------
> #
>
> CACHE_MIDDLEWARE_SECONDS = 300
> CACHE_MIDDLEWARE_KEY_PREFIX = 'XXX'
> CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
>
> ADMINS = (
>     # ('Your Name', '[EMAIL PROTECTED]'),
> )
>
> MANAGERS = ADMINS
>
> # Local time zone for this installation. Choices can be found here:
> #http://www.postgresql.org/docs/8.1/static/datetime-keywords.html#DATE...
> # although not all variations may be possible on all operating
> systems.
> # If running in a Windows environment this must be set to the same as
> your
> # system time zone.
> TIME_ZONE = 'Europe/Amsterdam'
>
> # Language code for this installation. All choices can be found here:
> #http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes
> #http://blogs.law.harvard.edu/tech/stories/storyReader$15
> LANGUAGE_CODE = 'nl'
>
> DATETIME_FORMAT = 'd-m-Y H:i'
>
> # If you set this to False, Django will make some optimizations so as
> not
> # to load the internationalization machinery.
> USE_I18N = True
>
> APPEND_SLASH = True
>
> # URL that handles the media served from MEDIA_ROOT.
> # Example: "http://media.lawrence.com";
> MEDIA_URL = ''
>
> # URL prefix for admin media -- CSS, JavaScript and images. Make sure
> to use a
> # trailing slash.
> # Examples: "http://foo.com/media/";, "/media/".
> ADMIN_MEDIA_PREFIX = '/media/'
>
> # Make this unique, and don't share it with anybody.
> SECRET_KEY = '[EMAIL PROTECTED]@d(5w+m)kpcjffne(pvb+#6w2s_pz*5)b%$f'
>
> # List of callables that know how to import templates from various
> sources.
> TEMPLATE_LOADERS = (
>     'django.template.loaders.filesystem.load_template_source',
>     'django.template.loaders.app_directories.load_template_source',
> #     'django.template.loaders.eggs.load_template_source',
> )
>
> TEMPLATE_CONTEXT_PROCESSORS = (
>     'django.core.context_processors.auth',
>     'django.core.context_processors.debug',
>     'django.core.context_processors.i18n',
>     'django.core.context_processors.request',
> )
>
> MIDDLEWARE_CLASSES = (
>     'django.middleware.common.CommonMiddleware',
>     'django.contrib.sessions.middleware.SessionMiddleware',
>     'django.contrib.auth.middleware.AuthenticationMiddleware',
>     'django.middleware.doc.XViewMiddleware',
>     'django.middleware.cache.CacheMiddleware',
>     'XXX.cms.middleware.session.ThreadLocals',
> )
>
> ROOT_URLCONF = 'rvucms.urls'
>
> INSTALLED_APPS = (
>     'XXX.cms',
>     'django.contrib.admin',
>     'django.contrib.auth',
>     'django.contrib.contenttypes',
>     'django.contrib.sessions',
>     'XXX.upload'
> )
>
> logging.basicConfig(level=LOG_LEVEL,
>                 format='%(asctime)s %(levelname)s %(message)s',
>                 filename=LOG_FILE,
>                 filemode='w')
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to