Django CACHE_BACKEND Error - django

So Im running into this CACHE error when I try to runserver or syncdb.
Here is the traceback: https://gist.github.com/1538051
I tried inserting this into the settings.py file:
CACHE_BACKEND = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
But that gave another error which makes no sense to me.
if backend_uri.find(':') == -1:
AttributeError: 'dict' object has no attribute 'find'
Can someone help me figure what the problem is and how I can go about fixing it.
NB: I am working on the dev server

If you're using Django 1.2 or lower, CACHE_BACKEND (docs) accepts a string:
CACHE_BACKEND = 'dummy://'
If you're using Django 1.3+, CACHE_BACKEND has been deprecated in favour of CACHES (docs):
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
I'm not sure why your installation isn't creating the appropriate default - it could be that there's something else going on with your installation, but I don't really have enough information without knowing more about your settings.py etc.

First off, What version of django are you using? The dictionary- style backend config is new to django 1.3, and your traceback suggests that you are on something like 1.2.4.
If that's the case, you will need to use the older uri-style cache settings. Something like
CACHE_BACKEND = 'dummy://'
would match what you are trying to set in your question.
The full documentation on caching, relevant to Django 1.2, is available here: http://docs.djangoproject.com/en/1.2/topics/cache/

Related

Is there a way to disable throttling while using Pytest in Django?

Problem:
I want to figure out a way to disable throttling when running my tests with pytest -vv
Details:
I have this default throttling policy in my settings.py file:
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '1000/day'
}
I also have this fixture in my confest.py which returns an error whenever I exceed the limit of requests:
def get_token(user, client):
response = client.post(
"/email-login",
{"email":user.email, "password": "B9vX95phJDi3C4"},
)
return {
"HTTP_AUTHORIZATION": f"Bearer {response.json()['token']['access']}"
}
What I have tried:
I have attempted to use the solution in this GitHub Issue: https://github.com/encode/django-rest-framework/issues/1336, but it doesn't work in my case.
First you need to create a way to differentiate between test env and otherwise. Like we do for PROD and DEV using settings.DEBUG config.
My recommendation is to create an env variable test=Trueand then in your settings.py write -
if os.environ.get("test", False):
REST_FRAMEWORK = {
'DEFAULT_THROTTLE_CLASSES': [
'rest_framework.throttling.AnonRateThrottle',
'rest_framework.throttling.UserRateThrottle'
],
'DEFAULT_THROTTLE_RATES': {
'anon': '100/day',
'user': '1000/day'
}
}
else it does nothing, and drf will not throttle.
I was able to resolve this problem with the following steps:
I created a new settings file which inherited from the base settings file. i.e from settings import *
Then I deleted the DEFAULT_THROTTLE_RATES key i.e del REST_FRAMEWORK["DEFAULT_THROTTLE_RATES"]
Next thing I did was to point to the new settings file in pytest.ini i.e DJANGO_SETTINGS_MODULE="new_settings.py"
Now the tests will use the new settings file
#ra123 has the right idea in general. As another approach, with all Django projects I add something like this to my settings/__init__.py (or just settings.py if you do a one file thing). It looks at argv to see if its in test mode
IS_TESTING = bool(set(sys.argv[:2]) & {"pytest", "test", "jenkins"})
REST_FRAMEWORK = { "YOUR_CONFIG": "..." }
# at the very very end, AFTER your settings are loaded:
if IS_TESTING:
# override your rest framework settings in test mode
REST_FRAMEWORK["DEFAULT_THROTTLE_CLASSES"] = []
# some other handy things, for making tests faster/easier
PASSWORD_HASHERS = ("django.contrib.auth.hashers.MD5PasswordHasher",)
EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend"
DEFAULT_FILE_STORAGE = "inmemorystorage.InMemoryStorage"
I ended up with it this way so we don't have to worry about it ever getting the wrong settings. It also helps keep things centralized, so (for example) you don't call sentry.init in testing mode, even if there is a sentry_url in the environment.

django alter settings for one app

Well, I have a django project which works fine now.
I'd like to add a new app to it, in which I need to access multiple databases.
I know Django support multiple databases settings and know how to configure it. This is not the problem.
The issue is that, in the 90% of my project components, I don't need to maintain a multiple databases settings. The only usage for the second databases is in the new added app.
So I tried to alter the settings by calling:
django.conf.settings.configure(DATABASES = {....})
in the new app. And django said:
RuntimeError: Settings already configured.
Which makes sense, since I have the origin settings file and set the DJANGO_SETTINGS_MODULE up.
So I question is that what should be a good approach in this case.
I don't want to discard DJANGO_SETTINGS_MODULE variable.
I try to not include second database in the setting file initially, since the new app is also an independent module which should work independently outside the django project. So I want to have the similar config in new app to setup the database config.
Does anyone has any idea about this?
Thanks in advance!
Actually, I have the same issue in a current project. As you I have a totally independent app which uses an another database, and I could have other apps which could have the same behaviour.
The thing that I have done is to create a dir apps where I store my apps and then I add this at the end of my settings.py file :
DATABASE_ROUTERS = ['myproject.routers.MultiDBRouter']
import os
APPS_DIR = os.path.join(PROJECT_ROOT, 'apps')
for app_name in os.listdir(APPS_DIR):
print '\nLooking for settings in apps/%s :' % app_name
if os.path.exists(os.path.join(APPS_DIR, app_name, 'settings.py')):
print ' Settings file found...'
app = __import__('%s.settings' % app_name)
content = dir(app.settings)
if 'DATABASES' in content:
print ' Adding databases :'
for key, value in app.settings.DATABASES.iteritems():
if DATABASES.has_key(key):
print ' Can not add %s database config, because it already exists' % key
else:
DATABASES[key] = value
DATABASES[key]['APPS'] = [app_name]
print ' Added %s database config' % key
It will automatically look after settings.py file in all the apps/myapp/ directories. If it finds a new DATABASES variable in a app/myapp/settings.py file, it will add the other database configurations to your DATABASES variable (the true one).
I have also created a router to do not have to use the using command (the MultiDBRouter).
And then I add a settings.py file in all the app which requires another database :
DATABASES = {
'db': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'database',
'USER': 'username',
'PASSWORD': 'mysecretpassword',
}
}

Django-CMS pages all blank after upgrade from 2.3.5 to 2.4.1

I am using Django 1.4.3 and have several sites built using Django-CMS 2.3.5. I want to upgrade them to Django-CMS 2.4.1, so that I can then start to move to Django 1.5.
When I type ./manage runserver from a virtualenv with Django-CMS 2.3.5, all works fine. When I move to a virtualenv with Django-CMS 2.4.1, but otherwise the same, none of my pages can be accessed.
In the admin panel, they all have the name None. If I edit one, the screen shows them with the correct name and all the plugins there. If I try to save the page, the info all disappears, including the name and slug and plugins, and I get a message at the top asking me to fix the below errors (of which there are none). I can press "publish draft", but the browser cannot find any page but /, and this has no plugins on it (and it still has name None in the admin panel).
If I try to add a new plugin, I get an alert saying <django.utils.functional.__proxy__ object at 0x1067a9e90>.
I am not using the MultilingualURLMiddleware middleware, and have USE_I18N = False.
I added 'django.middleware.locale.LocaleMiddleware' to MIDDLEWARE_CLASSES anyway (but it doesn't help if I do not).
I typed ./manage.py migrate, ./manage cms fix-mptt and for good measure ./manage.py cms delete_orphaned_plugins as well.
How do I keep my pages when I migrate to the new version of Django-CMS?
thanks
Here are upgrade instruction.
I think your problem is in new style CMS_LANGUAGES.
Try to set:
USE_I18N = True
CMS_LANGUAGES = {
1: [
{
'code': 'en',
'name': gettext('English'),
'public': True,
},
],
'default': {
'fallbacks': ['en',],
'public': False,
}
}
Also run ./manage.py cms check to checking django CMS installation
The most crucial thing I've ever learnt in django was taking manuel backup of the project.db before any migration process done by manage.py.

Getting django-facebook to work

I an trying to get django-facebook to work as per instructions given in the readme on
https://github.com/tschellenbach/Django-facebook. I am new to django.
It looks simple but I am facing the following problems. I am not able to get it to work.
In the readme it says AUTH_USER_MODEL = 'member.FacebookUser'. I am guessing the right option is
AUTH_USER_MODEL = 'django_facebook.FacebookUser'
after importing the models - this took me some t even after making that change, syncdb throws an error stating that:
FacebookUser does not have a USERNAME_FIELD.
Not able to solve that I decided to use the default user model - auth.user. That works and I was able to load facebook/example. However after authentication from facebook, I get an error
You need to set AUTH_PROFILE_MODULE in your project settings
So I added AUTH_PROFILE_MODULE = 'django_facebook.FacebookProfile'
Now it returns a new error -
FacebookProfile matching query does not exist. Lookup parameters were {'user_id_exact': 2L}
What should I do now?
Do python manage.py syncdb or whatever it needs to be done to update your database schema with the new table (facebook_profile).
Also, you don't mention it but I have and app that uses django_facebook and I have my settings.py file like this:
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django_facebook.context_processors.facebook',
...
)
AUTHENTICATION_BACKENDS = (
...
'django_facebook.auth_backends.FacebookBackend',
)
I hope it helps

Django-tinymce in Django Admin - "can't find variable: tinyMCE"

I'm trying to use django-tinymce to edit fields in django admin.
I've the app installed in my virtualenv (django-tinymce==1.5.1b4). It's listed in my installed apps -
INSTALLED_APPS = (
#...
'tinymce',
#...
)
My settings includes the following
TINYMCE_DEFAULT_CONFIG = {
'theme': "advanced",
'theme_advanced_toolbar_location': "top",
'theme_advanced_buttons1': "bold,italic,underline,separator,"
"bullist,separator,outdent,indent,separator,undo,redo",
'theme_advanced_buttons2': "",
'theme_advanced_buttons3': "",
}
TINYMCE_SPELLCHECKER = True
TINYMCE_COMPRESSOR = True
And I've got the files available at /MEDIA_ROOT/js/tiny_mce (the default).
My models look like -
from tinymce import models as tinymce_models
class MyModel(models.Model)
post = tinymce_models.HTMLField()
When I go to the model admin page, the field appears as a normal text field and my browser tells me there's an error on the inline js script for the field. It says it doesn't recognise the variable tinyMCE. It doesn't look like the page is even trying to load the js file (I'm getting no 404's - I can't see any sign of anything being loaded).
I'm not sure what I'm missing..
Have You, Sir, done python manage.py collectstatic ?
What value variable in settings.py is in TINYMCE_JS_ROOT and TINYMCE_JS_URL
If variable TINYMCE_JS_URL is not set check if You, Sir, have file at /MEDIA_ROOT/js/tiny_mce/tiny_mce.js. If not, try to copy manually from django-tinymce's egg.
OK, looks like it might have been a bug in the django_tinymce code. I've reverted to 1.5.1b2 and everything works as expected. Guess I should look into filing a bug report.