I have been following this tutorial so that I can use Memcache on my app on Heroku. However I ran into issues when using cache.get() in Heroku's shell (it works fine on my end):
File "/app/.heroku/python/lib/python3.4/site-packages/django_pylibmc/memcached.py", line 92
except MemcachedError, e:
^
SyntaxError: invalid syntax
I saw this question, who had the same issue as me. My settings.py looked like this:
def get_cache():
import os
try:
os.environ['MEMCACHE_SERVERS'] = os.environ['MEMCACHIER_SERVERS'].replace(',', ';')
os.environ['MEMCACHE_USERNAME'] = os.environ['MEMCACHIER_USERNAME']
os.environ['MEMCACHE_PASSWORD'] = os.environ['MEMCACHIER_PASSWORD']
return {
'default': {
'BACKEND': 'django_pylibmc.memcached.PyLibMCCache',
'TIMEOUT': 500,
'BINARY': True,
'OPTIONS': { 'tcp_nodelay': True }
}
}
except:
return {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'
}
}
And so I replaced django_pylibmc.memcached.PyLibMCCache with django.core.cache.backends.memcached.PyLibMCCache. However then I got a different error when I tried cache.get("foo") again:
File "/app/.heroku/python/lib/python3.4/site-packages/django/core/cache/backends/memcached.py", line 84, in get
val = self._cache.get(key)
_pylibmc.ConnectionError: error 3 from memcached_get(:1:foo): (0x1c4ed40) CONNECTION FAILURE, host: localhost:11211 -> libmemcached/get.cc:314
Could someone help me out? Is there some settings somewhere that I need to change?
The syntax
except MemcachedError, e:
works in Python 2 but not Python 3.
Change it to except MemcachedError as e: and you should be fine.
I decided to switch to using Django's database cache instead.
Related
I am trying to connect AstraDB with my application served by gcloud AppEngine. I am using Django and therefore have used django_cassandra_engine. I want to keep Postgres as my default DB and use cassandra as a second DB.
Everything works as expected on localhost,but when I deploy to gcloud, I receive 502 Bad gateway error and in the logs it says:
cassandra.cqlengine.CQLEngineException: Connection name 'cassandra'
doesn't exist in the registry.
I am using:
Django==4.1
django-cassandra-engine==1.7.0
cassandra-driver==3.25.0
My secure_connect_bundle (ZIP file) is in the same folder where manage.py is located.
This is my settings.py:
# [START db_setup]
# [START gaestd_py_django_database_config]
# Use django-environ to parse the connection string
DATABASES = {
"default": env.db(),
'cassandra': {
'ENGINE': 'django_cassandra_engine',
'NAME': 'the_keyspace',
'TEST_NAME': 'test_db',
'OPTIONS': {
'connection': {
'auth_provider': PlainTextAuthProvider(username=env("ASTRA_CLIENT_ID"),password=env("ASTRA_SECRET")),
'cloud': {
'secure_connect_bundle': os.path.join(BASE_DIR, "secure-connect-db.zip")
}
}
}
}
}
# If the flag as been set, configure to use proxy
if os.getenv("USE_CLOUD_SQL_AUTH_PROXY", None):
DATABASES["default"]["HOST"] = "127.0.0.1"
DATABASES["default"]["PORT"] = 5432
# [END gaestd_py_django_database_config]
# [END db_setup]
# Use a in-memory sqlite3 database when testing in CI systems
# TODO(glasnt) CHECK IF THIS IS REQUIRED because we're setting a val above
if os.getenv("TRAMPOLINE_CI", None):
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
}
}
Just a thought, but I don't think you need the extra cassandra group in the DATABASES section. Try this:
DATABASES = {
"default": {
'ENGINE': 'django_cassandra_engine',
'NAME': 'brondau_keyspace',
We (DataStax) actually ran a workshop showing Django and Astra DB in December: https://www.youtube.com/watch?v=tUF_dX0lgt0
The Git repo can be found here: Build A Blog in Python with Django and Astra DB NoSQL Database
This section of the repo should help with your current issue: https://github.com/tomitokko/django-blog-with-astradb/blob/ddc1c2d608e8fc0d6569796775cdfaf503afab94/techblog/settings.py#L92
Anyway, give my suggestion a try and check the video and/or repo for additional help.
So the problem was that secure_connect_bundle (ZIP file) couldn't be extracted on AppEngine.
Setting 'use_default_tempdir' to True in cloud_config solves the issue.
If you are using python driver to establish connection, it should look like this:
cloud_config= {
'secure_connect_bundle': ASTRADB_CONNECT_BUNDLE,
'use_default_tempdir': True
}
Or if you use django_cassandra_engine:
'OPTIONS': {
'connection': {
'auth_provider': PlainTextAuthProvider(username=env("ASTRA_CLIENT_ID"),password=env("ASTRA_SECRET")),
'cloud': {
'secure_connect_bundle': 'secure-brondau-db.zip',
'use_default_tempdir': True
}
}
}
While working a migration of an older Django project I ran into this error after running:
python manage.py check
cms.UserSettings.language: (fields.E005) 'choices' must be an iterable containing (actual value, human readable name) tuples.
Has anyone run into this issue? Unfortunately I have to wait until I am not on the corp network before I can ask the IRC channels.
http://docs.django-cms.org/en/latest/reference/configuration.html#cms-languages
It turns out I missed this important setting in my settings.py file:
CMS_LANGUAGES = {
'default': {
'fallbacks': ['en',],
'redirect_on_fallback':True,
'public': True,
'hide_untranslated': False,
}
}
Thanks to brianpck for a point in the right direction though.
I'm trying to implement memcached compare-and-set pattern, following the instructions of Guido at:
http://neopythonic.blogspot.nl/2011/08/compare-and-set-in-memcache.html
However, I don't seem to get it right and I have no idea what's wrong. The files below use Django (1.4.5 Final) and python-memcache (1.48).
settings.py
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
djangocache.py
#!/usr/bin/env python
from django.core.cache import cache
import multiprocessing.dummy
django_key = "TEST"
cached_key = cache.make_key(django_key).encode("UTF-8")
def add_to_cache(item):
client = cache._cache
#client = cache._lib.Client(cache._servers)
while True:
items = client.gets(cached_key)
if client.cas(cached_key, items+(item,)):
break
if __name__ == "__main__":
cache.set(django_key, ())
p = multiprocessing.dummy.Pool(2)
p.map(add_to_cache, range(10))
print(len(cache.get(django_key)))
Running it:
mzialla#Q330 ~/test $ DJANGO_SETTINGS_MODULE=settings python djangocache.py
5
It occasionally outputs 6, 7, etc. like you would expect when dealing with race conditions. I've tried multiple client instantiations (see comment).
Help?
python-memcached disables cas by default. Enable it by adding
client.cache_cas = True
to your code.
Credits to Nate Thelen, who's comment I discovered right after asking this question.
I have a Django 1.4.3 project using Python 2.7.3 on Ubuntu 12.10. In my entire project, there is only one settings.py file, and in manage.py, I have this line
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")
Of course, the name of my project is hellodjango, and the settings.py file is in the subfolder hellodjango (they have the same name).
When I type the following line into the console:
python manage.py diffsettings
These are my results for DATABASES:
DATABASES = {'default': {}, 'mioa': {'1': 'this thingy', '2': 'that thingy'}}
The relevant lines in settings.py are these:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'mydatabase'
},
'mioa': {
'1': 'this thingy',
'2': 'that thingy'
}
}
I have the 'mioa' entry in there to demonstrate my problem. For some reason, the 'default' field is not getting set. Does anyone know why this is happening? Or what the solution is?
Help on diffsettings says:
Displays differences between the current settings.py and Django's
default settings
which means, that it show only difference between your value and default one. Seems like your value is completely the same as the default one, so you don't see any difference.
Try another engine and/or database name and you'll see it's changed.
I'm having some issues setting up django-mssql on Win Server 2008 R2. I have everything installed, however, the wiki for django-mssql says to setup the settings file similar to:
DATABASES = {
'default': {
'NAME': 'my_database',
'ENGINE': 'sqlserver_ado',
'HOST': 'dbserver\\ss2008',
'USER': '',
'PASSWORD': '',
'OPTIONS' : {
'provider': 'SQLOLEDB',
'use_mars': True,
},
}
}
When I run from my site directory:
python manage.py syncdb
I get an error stating it isn't an available database backend. When I installed django-mssql it seemed to install the backend here \site-packages\django_mssql-1.0.1-py2.7.egg\sqlserver_ado does this need to be copied to site-packages\django\db\backends?
I get the same error if I set my settings to:
DATABASES = {
'default': {
'NAME': 'my_database',
'ENGINE': 'django_mssql-1.0.1-py2.7.egg.sqlserver_ado',
'HOST': 'dbserver\\ss2008',
'USER': '',
'PASSWORD': '',
'OPTIONS' : {
'provider': 'SQLOLEDB',
'use_mars': True,
},
}
}
Am I missing something when setting up this backend? This is my first time using django, but I didn't see anything in the documentation for setting up a different backend, and the django-mssql wiki or issues doesn't seem to have anything either.
Also, if there is other documentation somewhere that can help please let me know.
EDIT: The django app is running on Ubuntu server.
Dustin's comment about making sure "import sqlserver_ado" from the command shell got me going down the right path on my Django 1.8.1, Python 3.5 Win32 system with pywin32 installed.
SPOILER ALERT This only gets my Django instance to run without errors. I haven't tested the ADO connection yet.
The first error message I got was:
No module named 'django.db.backends.util'
and I found there is a file called: django.db.backends.utils so I copied it and renamed it to django.db.backends.util (without the 's') and away went the error message!
So hoping this wasn't too harmful, I continued on this line of troubleshooting.
The next error message I got was:
File "C:\Program Files (x86)\Python35-32\lib\site-packages\sqlserver_ado\base.py", line 7, in <module>
from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseValidation, BaseDatabaseClient
ImportError: cannot import name 'BaseDatabaseWrapper'
I changed line 7 in base.py to now say:
#from django.db.backends import BaseDatabaseWrapper, BaseDatabaseFeatures, BaseDatabaseValidation, BaseDatabaseClient
from django.db.backends.base.base import BaseDatabaseWrapper
from django.db.backends.base.features import BaseDatabaseFeatures
from django.db.backends.base.validation import BaseDatabaseValidation
from django.db.backends.base.client import BaseDatabaseClient
Yes, that's commenting out the bad line and adding four separate lines.
Then I got this error:
File "C:\Program Files (x86)\Python35-32\lib\site-packages\sqlserver_ado\base.py", line 18, in <module>
from .introspection import DatabaseIntrospection
File "C:\Program Files (x86)\Python35-32\lib\site-packages\sqlserver_ado\introspection.py", line 3, in
from django.db.backends import BaseDatabaseIntrospection
ImportError: cannot import name 'BaseDatabaseIntrospection'
so I changed the line 3 to now read:
from django.db.backends.base.introspection import BaseDatabaseIntrospection
and so on for creation.py:
from django.db.backends.base.creation import BaseDatabaseCreation
for operations.py:
from django.db.backends.base.operations import BaseDatabaseOperations
for schema.py:
from django.utils.log import getLogger
Hope this helps someone. Hope the ADO module actually connects to something.
-Sean
You will want to make sure that you can import "sqlserver_ado" from your python shell.
Put the folder sqlserver_ado somewhere on your PATH, I put mine in \site-packages\
Take a look at the README.txt.
The engine does want to be set to "sqlserver_ado" similar to how the settings are done on the settings sample page.
As of 2019:
I couldn't get Django MSSQL to work at all.
I switched over to django-pyodbc-azure and that works great.
Install:
pip install django-pyodbc-azure
Setup:
'ENGINE': 'sql_server.pyodbc'
You need to install the dependency PyWin32. You can install via pip or download from the python binaries page http://www.lfd.uci.edu/~gohlke/pythonlibs/
I was trying to get django_pyodbc to work, and couldn't. I was getting this error:
django.core.exceptions.ImproperlyConfigured: 'django_pyodbc' isn't an available database backend.
Try using 'django.db.backends.XXX', where XXX is one of:
u'base', u'mysql', u'oracle', u'postgresql_psycopg2', u'sqlite3'
Error was: cannot import name BaseDatabaseWrapper
I was directed to this post as a solution, so I'll post my answer here also.
I downgraded to django 1.6 instead of 1.8, and now django_pyodbc works as a database backend.
As per https://github.com/lionheart/django-pyodbc/pull/96, django_pyodbc should now work with Django 1.8. So this seems to be a good alternative to django-mssql for those requiring SQL Server 2008 R2 support.