How do I replace MemcachedCache with PyMemcacheCache in Django? - python

I'm running my website on Django 3.2. I read in Django’s cache framework that MemcachedCache and python-memcached are deprecated. I installed pymemcache==3.5.0 on my staging server and changed to CACHE_URL=pymemcache://127.0.0.1:11211 in env.ini. But if I uninstall python-memcached with pip I receive an error message, that indicates that MemcachedCache is still used by my code, and it fails on import memcache.
My code uses the following imports:
from django.core.cache import cache
from django.core.cache.backends.base import DEFAULT_TIMEOUT
How do I replace MemcachedCache with PyMemcacheCache so that MemcachedCache will not be used in my code?
I'm using django-environ==0.8.1 and CACHES is defined in my settings:
CACHES = {
'default': env.cache()
}
env is defined as environ.Env().
My Traceback:
Environment:
Request Method: GET
Request URL: https://<...>
Django Version: 3.2.10
Python Version: 3.8.12
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'crispy_forms',
'friendship',
'rules.apps.AutodiscoverRulesConfig',
'sorl.thumbnail',
'speedy.core.base',
'speedy.core.accounts',
'speedy.core.blocks',
'speedy.core.uploads',
'speedy.core.messages',
'speedy.core.profiles',
'speedy.core.friends',
'speedy.core.about',
'speedy.core.privacy',
'speedy.core.terms',
'speedy.net.accounts',
'speedy.match.accounts',
'speedy.match.likes',
'speedy.composer.accounts',
'speedy.mail.accounts',
'speedy.core.contact_by_form',
'speedy.match.profiles',
'speedy.match.matches']
Installed Middleware:
['speedy.core.base.middleware.SessionCookieDomainMiddleware',
'speedy.core.base.middleware.RemoveExtraSlashesMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.contrib.sites.middleware.CurrentSiteMiddleware',
'django.middleware.locale.LocaleMiddleware',
'speedy.core.base.middleware.LocaleDomainMiddleware',
'speedy.core.base.middleware.EnsureCachesMiddleware',
'speedy.core.base.middleware.UpdateSessionAuthHashMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'speedy.core.accounts.middleware.SiteProfileMiddleware']
Traceback (most recent call last):
File "<...>/env/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "<...>/./speedy/core/base/middleware.py", line 139, in __call__
block_managers.ensure_caches(user=request.user)
File "<...>/./speedy/core/blocks/managers.py", line 46, in ensure_caches
blocked_entities = cache_manager.cache_get(blocked_key, sliding_timeout=DEFAULT_TIMEOUT)
File "<...>/./speedy/core/base/cache_manager.py", line 21, in cache_get
wrapped_value = cache.get(key, default=DEFAULT_VALUE, version=version)
File "<...>/env/lib/python3.8/site-packages/django/utils/connection.py", line 15, in __getattr__
return getattr(self._connections[self._alias], item)
File "<...>/env/lib/python3.8/site-packages/django/utils/connection.py", line 62, in __getitem__
conn = self.create_connection(alias)
File "<...>/env/lib/python3.8/site-packages/django/core/cache/__init__.py", line 44, in create_connection
return backend_cls(location, params)
File "<...>/env/lib/python3.8/site-packages/django/core/cache/backends/memcached.py", line 181, in __init__
import memcache
Exception Type: ModuleNotFoundError at /matches/
Exception Value: No module named 'memcache'
My repository: https://github.com/speedy-net/speedy-net
Update: I checked the email message I received for debugging and I see that the CACHES in settings is equal to {'default': {'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': '127.0.0.1:11211'}}.
I submitted an issue (possibly a bug) on https://github.com/joke2k/django-environ/issues/359

I don't know why, but the way I restarted my server during deploy doesn't refresh env.ini, and the server remembers the old settings. If I restart my server with sudo reboot, with the same settings, then CACHES is equal to {'default': {'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache', 'LOCATION': '127.0.0.1:11211'}} and the site works properly. And I confirmed that if I put dummy values in env.ini then the site doesn't work at all. But only if I restart my server with sudo reboot.

Related

Invalid host header in django

Environment:
Request Method: GET
Request URL: http://abampdb.......nust.edu.pk:83/
Django Version: 4.0.2
Python Version: 3.9.12
Installed Applications:
['abampdb.apps.AmpdbConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "/home/abampdb/projectdir/venv_name/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/abampdb/projectdir/venv_name/lib/python3.9/site-packages/django/utils/deprecation.py", line 125, in __call__
response = self.process_request(request)
File "/home/abampdb/projectdir/venv_name/lib/python3.9/site-packages/django/middleware/common.py", line 48, in process_request
host = request.get_host()
File "/home/abampdb/projectdir/venv_name/lib/python3.9/site-packages/django/http/request.py", line 135, in get_host
raise DisallowedHost(msg)
Exception Type: DisallowedHost at /
Exception Value: Invalid HTTP_HOST header: 'abampdb.......t.edu.pk:83'. You may need to add 'abampdb...........du.pk' to ALLOWED_HOSTS.
I have used nginx and gunicorn for the deployement from this link https://djangocentral.com/deploy-django-with-nginx-gunicorn-postgresql-and-lets-encrypt-ssl-on-ubuntu/.
I have edited the settings.py also with the domain name in the Allowed Host but still getting the same error and after editing i have also run the gunicorn restart command and nginx reload command. here is my

Simple todolist Bradtraversy lower limit error

I am trying to run the bradtraversy simple todo list django code on my raspberry pi and have run into the following error. I have run into the following traceback:
Environment:
Request Method: GET
Request URL: HIDDEN
Django Version: 1.11.11
Python Version: 2.7.13
Installed Applications:
['todos.apps.TodosConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py" in inner
41. response = get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/utils/deprecation.py" in __call__
138. response = self.process_request(request)
File "/usr/local/lib/python2.7/dist-packages/django/middleware/common.py" in process_request
57. host = request.get_host()
File "/usr/local/lib/python2.7/dist-packages/django/http/request.py" in get_host
105. if domain and validate_host(domain, allowed_hosts):
File "/usr/local/lib/python2.7/dist-packages/django/http/request.py" in validate_host
579. if pattern == '*' or is_same_domain(host, pattern):
File "/usr/local/lib/python2.7/dist-packages/django/utils/http.py" in is_same_domain
291. pattern = pattern.lower()
Exception Type: AttributeError at /
Exception Value: 'list' object has no attribute 'lower'
I am fairly new to Django but thought I made all the correct adjustments to match my system specifics. I updated the settings file to match my allowed hosts and database setting and used the "python manage.py migrate" command to bring over migrations.
Any thoughts or help would be greatly appreciated. Thank you

Bitnami Django Stack and module "requests": cannot import name 'certs'

Very specific stuff. I'm running a Bitnami Django stack cloud VM on Amazon. On two different "regular" machines, I could install requests by running sudo pip install requests, but it seems that Bitname uses it's own specific structure, and something is going wrong when installing requests that way. It can also be related to issue #2028, but it was fixed long time ago.
I have the following traceback:
Environment:
Request Method: GET
Request URL: http://54.94.226.137/
Django Version: 1.7.7
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'vz_base',
'vz_api',
'vz_admin',
'vz_user')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
98. resolver_match = resolver.resolve(request.path_info)
File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/urlresolvers.py" in resolve
343. for pattern in self.url_patterns:
File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/urlresolvers.py" in url_patterns
372. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/opt/bitnami/python/lib/python2.7/site-packages/django/core/urlresolvers.py" in urlconf_module
366. self._urlconf_module = import_module(self.urlconf_name)
File "/opt/bitnami/python/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/opt/bitnami/apps/django/django_projects/ProjetoVigZul/ProjetoVigZul/urls.py" in <module>
5. from vz_api import urls as api_urls
File "/opt/bitnami/apps/django/django_projects/ProjetoVigZul/vz_api/urls.py" in <module>
8. from vz_api import views
File "/opt/bitnami/apps/django/django_projects/ProjetoVigZul/vz_api/views.py" in <module>
14. from vz_api.dguard import DGuardManager, DGuardIntegrationException
File "/opt/bitnami/apps/django/django_projects/ProjetoVigZul/vz_api/dguard.py" in <module>
5. import requests, json
File "/opt/bitnami/python/lib/python2.7/site-packages/requests/__init__.py" in <module>
58. from . import utils
File "/opt/bitnami/python/lib/python2.7/site-packages/requests/utils.py" in <module>
25. from . import certs
Exception Type: ImportError at /
Exception Value: cannot import name certs
Website is live at http://54.94.226.137/, if anyone is interested.
sudo pip install --upgrade requests
solved that for me

Django Rest Framework - 'module' object has no attribute 'HStoreField'

I recently updated a project that was working with Django rest framework 2.4.2 to the newly 3.1 version but now I'm getting a:
'module' object has no attribute 'HStoreField'
That happens to all the endpoints including the "/"
Any suggestions?
my Django version is 1.8 and python 2.7.6
Traceback:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/
Django Version: 1.8.dev20140910173004
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest',
'rest_framework',
'geoposition',
'rest_framework_swagger',
'PIL',
'rest_framework.authtoken',
'corsheaders')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')
Traceback:
File "/Users/marcus/PycharmProjects/django-framework/django-trunk/django/core/handlers/base.py" in get_response
112. resolver_match = resolver.resolve(request.path_info)
File "/Users/marcus/PycharmProjects/django-framework/django-trunk/django/core/urlresolvers.py" in resolve
362. for pattern in self.url_patterns:
File "/Users/marcus/PycharmProjects/django-framework/django-trunk/django/core/urlresolvers.py" in url_patterns
398. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/marcus/PycharmProjects/django-framework/django-trunk/django/core/urlresolvers.py" in urlconf_module
392. self._urlconf_module = import_module(self.urlconf_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/Users/marcus/Google Drive/Spring2015/AppContest/MyApplication/Backend/Backend/urls.py" in <module>
3. from rest_framework import routers
File "/Library/Python/2.7/site-packages/rest_framework/routers.py" in <module>
23. from rest_framework import views
File "/Library/Python/2.7/site-packages/rest_framework/views.py" in <module>
91. class APIView(View):
File "/Library/Python/2.7/site-packages/rest_framework/views.py" in APIView
94. renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES
File "/Library/Python/2.7/site-packages/rest_framework/settings.py" in __getattr__
206. val = perform_import(val, attr)
File "/Library/Python/2.7/site-packages/rest_framework/settings.py" in perform_import
158. return [import_from_string(item, setting_name) for item in val]
File "/Library/Python/2.7/site-packages/rest_framework/settings.py" in import_from_string
170. module = importlib.import_module(module_path)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py" in import_module
37. __import__(name)
File "/Library/Python/2.7/site-packages/rest_framework/renderers.py" in <module>
20. from rest_framework import exceptions, serializers, status, VERSION
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py" in <module>
1354. ModelSerializer.serializer_field_mapping[postgres_fields.HStoreField] = CharMappingField
Exception Type: AttributeError at /
Exception Value: 'module' object has no attribute 'HStoreField'
Installing the latest release candidate of Django 1.8 should fix the issue.
You are running 1.8.dev20140910173004, which already has support for some Postgres fields, but not for HStoreField yet. Django REST framework only checks for the django.contrib.postgres and then assumes that HStoreField is available too. For your version of Django that is not the case.
If pip install -u doesn't get you the latest version, try removing and reinstalling Django.

Django Python 1.5.1 throwing import error

I am new to Django and following the tutorial mentioned in this link:
http://www.djangobook.com/en/2.0/index.html
Everything goes well till I come to the Chapter 3, section of creating the Hello World:
http://www.djangobook.com/en/2.0/chapter03.html
I believe I have followed everything to the letter - but when I try to access the site:
http://127.0.0.1:8000/hello/
I get the following error:
ImportError at /hello/
No module named views
Here is the traceback information for this error:
Environment:
Request Method: GET
Request URL: `http://127.0.0.1:8000/hello/`
Django Version: 1.5.1
Python Version: 2.7.5
Installed Applications:
('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Traceback:
File "E:\DPF\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
103. resolver_match = resolver.resolve(request.path_info)
File "E:\DPF\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
319. for pattern in self.url_patterns:
File "E:\DPF\Python27\lib\site-packages\django\core\urlresolvers.py" in url_patterns
347. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "E:\DPF\Python27\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
342. self._urlconf_module = import_module(self.urlconf_name)
File "E:\DPF\Python27\lib\site-packages\django\utils\importlib.py" in import_module
35. __import__(name)
File "E:\Work\Documents\Sites\mysite\mysite\urls.py" in <module>
2. from mysite.views import hello
Exception Type: ImportError at /hello/
Exception Value: No module named views
The directory structure is as follows:
mysite--
mysite--
__init__.py
__init__.pyc
settings.py
settings.pyc
urls.py
urls.pyc
wsgi.py
wsgi.pyc
django.db
manage.py
views.py
Any idea what could have gone wrong?
Regards,
Indrajit
did you added <appname> to INSTALLED_APPS in settings.py
still errror then,
if you run django-admin.py startapp <appneam> from the project folder(\<mainfolder>\<projectfolder>)
the you want to try:
from <yourprojectname>.<yourappname>.views import hello
or you run django-admin.py startapp <appneam> from the folder contains manage.py(ie the \<mainfolder>\).
then do all the nessasary steps and then try:
from <yourappname>.views import hello
actually the djangobook is old and not updated frequently.

Categories

Resources