It was a silly glitch. I found out that i needed to reload gunicorn server to make the new middleware work. Thanks everybody for the help.
New Django middleware not getting called
-
04-12-2021 - |
题
I am quite new to web development. I am working on a website hosted on amazon ec2 server. The site is in python using django framework. I am using memcached to cache some client information. My site and caching works on local machine but not on the EC2 server. I checked memcached server and found out that it was not able to set the keys. Is there something I might need to change in settings.py so that keys are set appropriately on the server or something else that I might be missing.
EDIT: Found out the problem. I added a new middleware for setting keys in the memcache. That is not getting called. It works perfectly on the local machine. On the server I am using gunicorn as the app server and nginx as the reverse proxy. Can any of these cause the problems. Also I tried to reload nginx but that didn't help either.
解决方案
其他提示
Is it a case of add this to your settings.py?
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
)
From the docs.
Only I'm not too clear what steps you mean when you say you 'added a new middleware'.
First of all, you need to set caching in CACHES
setting, taking into account all credentials needed to access the cache server: https://docs.djangoproject.com/en/dev/topics/cache/#memcached
Also make sure the cache server is accessible from the instance you are trying to use.
When it comes to using new middleware (a slightly different issue), then:
- make sure it is defined properly in the settings (not overwritten by other settings, like eg. commonly used
local_settings.py
file, which should be different depending on the environment you use), - make sure middleware class is defined properly (defines eg.
process_request()
, if you want to do something for the incoming request), - if you are using eg. mod_wsgi with Apache, remember to reload the service (in this case Apache), by eg. invoking
sudo service apache2 reload
(as in the mentioned case),