`from django.conf import settings` is not working - python

I've changed my manage.py and settings.py and added a wsgi.py file to bring my Django 1.4 project closer to the modern age. This was done primarily because django_gunicorn is now deprecated. I'm using Python 2.7.9 and Django 1.4.19 in a PyCharm 4.0.6 development environment.
I also changed my directory structure from flat to nested.
/gse
manage.py
/gse
__init__.py
settings.py
urls.py
wsgi.py
...
/app1
/app2
manage.py
from os import environ
import sys
if __name__ == "__main__":
environ.setdefault("DJANGO_SETTINGS_MODULE", "gse.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
wsgi.py
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "gse.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
I installed django-avatar a while back, and now after the changes mentioned above it is breaking when I run manage.py validate.
I get the following traceback in PyCharm:
Traceback (most recent call last):
File "C:\Program Files (x86)\JetBrains\PyCharm 4.0.6\helpers\pycharm\django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)
File "C:\Python27\Lib\runpy.py", line 176, in run_module
fname, loader, pkg_name)
File "C:\Python27\Lib\runpy.py", line 82, in _run_module_code
mod_name, mod_fname, mod_loader, pkg_name)
File "C:\Python27\Lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "C:\Users\nu\gse\manage.py", line 23, in <module>
execute_from_command_line(sys.argv)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\__init__.py", line 443, in execute_from_command_line
utility.execute()
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\base.py", line 232, in execute
output = self.handle(*args, **options)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\base.py", line 371, in handle
return self.handle_noargs(**options)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\commands\validate.py", line 9, in handle_noargs
self.validate(display_num_errors=True)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\management\validation.py", line 30, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\db\models\loading.py", line 158, in get_app_errors
self._populate()
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\db\models\loading.py", line 64, in _populate
self.load_app(app_name, True)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\db\models\loading.py", line 88, in load_app
models = import_module('.models', app_name)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\utils\importlib.py", line 35, in import_module
__import__(name)
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\avatar\models.py", line 23, in <module>
avatar_storage = get_storage_class(settings.AVATAR_STORAGE)()
File "C:\Users\nu\gseVirtualEnv\lib\site-packages\django\core\files\storage.py", line 149, in __init__
self.location = abspathu(self.base_location)
File "C:\Users\nu\gseVirtualEnv\lib\ntpath.py", line 487, in abspath
path = _getfullpathname(path)
TypeError: coercing to Unicode: need string or buffer, tuple found
Process finished with exit code 1
I noticed that from django.conf import settings is used in django-avatars conf.py file. I tested the import statement in my project and discovered that PyCharm doesn't seem to be able to list the expected member variables from the settings class when I type settings.. My settings file correctly associated with Django in PyCharm.

Thank you Aamir.
When I looked at settings.py again I saw this:
MEDIA_ROOT = (os.path.join(os.path.dirname(__file__), '..', 'media').replace('\\','/'),)
AVATAR_STORAGE = MEDIA_ROOT + '\avatars'
Changing the MEDIA_ROOT assignment line to:
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), '..', 'media').replace('\\','/')
Fixed it.

Related

ImportError: cannot import name caches

I am using Django 1.6. I want to start working site. When you use python manage.py shell get an error:
(test) D:\test>python manage.py shell
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_from_command_line(sys.argv)
File "D:\testvenv\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
utility.execute()
File "D:\testvenv\lib\site-packages\django\core\management\__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "D:\testvenv\lib\site-packages\django\core\management\base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "D:\testvenv\lib\site-packages\django\core\management\base.py", line 280, in execute
translation.activate('en-us')
File "D:\testvenv\lib\site-packages\django\utils\translation\__init__.py", line 130, in activate
return _trans.activate(language)
File "D:\testvenv\lib\site-packages\django\utils\translation\trans_real.py", line 188, in activate
_active.value = translation(language)
File "D:\testvenv\lib\site-packages\django\utils\translation\trans_real.py", line 177, in translation
default_translation = _fetch(settings.LANGUAGE_CODE)
File "D:\testvenv\lib\site-packages\django\utils\translation\trans_real.py", line 159, in _fetch
app = import_module(appname)
File "D:\testvenv\lib\site-packages\django\utils\importlib.py", line 40, in import_module
__import__(name)
File "D:\testvenv\lib\site-packages\constance\backends\database\__init__.py", line 1, in <module>
from django.core.cache import caches
ImportError: cannot import name caches
Tell me what I have a problem. And if it can depend on your database (not sure that Django is connected to the DB - I do not know how to check)?
Added:
Code manag.py:
#!/usr/bin/env python
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
By the looks of it you're using django-constance.
According to their change log, they haven't supported 1.6 since version 1.1
So either use an older version of that, or better yet - use a supported version of django.
According to the release notes, django.core.cache.caches was introduced in Django 1.7, so this application has never worked with Django 1.6.
If you have a running instance of that application, check which version of Django it actually uses.

django deploy- ImproperlyConfigured: The SECRET_KEY setting must not be empty

The project worked fine on windows pycharm.
Now i am putting it on ubuntu14.04 and apache2.
When i run python manage.py runserver 8000
Traceback (most recent call last): File "manage.py", line 14, in <module>
execute_from_command_line(sys.argv) File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/core/management/__init__.py", line 350, in execute_from_command_line
utility.execute() File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/core/management/__init__.py", line 342, in execute
self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/core/management/__init__.py", line 195, in fetch_command
klass = load_command_class(app_name, subcommand) File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/core/management/__init__.py", line 39, in load_command_class
module = import_module('%s.management.commands.%s' % (app_name, name)) File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name) File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/core/management/commands/runserver.py", line 16, in <module>
from django.db.migrations.executor import MigrationExecutor File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/db/migrations/executor.py", line 7, in <module>
from .loader import MigrationLoader File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/db/migrations/loader.py", line 10, in <module>
from django.db.migrations.recorder import MigrationRecorder File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/db/migrations/recorder.py", line 12, in <module>
class MigrationRecorder(object): File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/db/migrations/recorder.py", line 26, in MigrationRecorder
class Migration(models.Model): File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/db/migrations/recorder.py", line 27, in Migration
app = models.CharField(max_length=255) File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/db/models/fields/__init__.py", line 1072, in __init__
super(CharField, self).__init__(*args, **kwargs) File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/db/models/fields/__init__.py", line 166, in __init__
self.db_tablespace = db_tablespace or settings.DEFAULT_INDEX_TABLESPACE File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/conf/__init__.py", line 55, in __getattr__
self._setup(name) File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/conf/__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module) File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/conf/__init__.py", line 99, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE) File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name) File "/var/www/Web-Interaction-APP/settings.py", line 8, in <module>
application = get_wsgi_application() File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/core/wsgi.py", line 13, in get_wsgi_application
django.setup() File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/__init__.py", line 17, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/conf/__init__.py", line 55, in __getattr__
self._setup(name) File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/conf/__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module) File "/usr/local/lib/python2.7/dist-packages/Django-1.9-py2.7.egg/django/conf/__init__.py", line 120, in __init__
raise ImproperlyConfigured("The SECRET_KEY setting must not be empty.") django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.
I have also tried to change my manage.py and wsgi.py. My folder is:
Web-Interaction-APP
-manage.py
-seetings.py
*apache(folder)
-wsgi.py
*project(folder)
in my manage.py:
#!/usr/bin/env python
#This file runs the server for this application from command line.
import os, sys
#Executes the code in the following condition if the program is run directly.
#(Source: http://stackoverflow.com/questions/419163/what-does-if-name-main-do)
if __name__ == "__main__":
#Refers to the path of this django project's settings module.
#(DJANGO_SETTINGS_MODULE is an environment variable to module import path.)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
from django.core.management import execute_from_command_line
#Executes the django project.
execute_from_command_line(sys.argv)
In my wsgi.py:
import os, sys
# Calculate the path based on the location of the WSGI script.
apache_configuration= os.path.dirname(__file__)
project = os.path.dirname(apache_configuration)
workspace = os.path.dirname(project)
sys.path.append(workspace)
sys.path.append(project)
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Web-Interaction-APP.settings")
# Add the path to 3rd party django application and to django itself.
sys.path.append('/home/zhaojf1')
os.environ['DJANGO_SETTINGS_MODULE'] = '10.231.52.XX.apache.override'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
Maybe in your settings file, you are setting the SECRET_KEY using a ENV variable, open a shell and just:
export SECRET_KEY='test_key'
Then, try to run your server again.
This error is common when your DJANGO_SETTINGS_MODULE path is incorrect.
When you run python manage.py runserver 8000 you are using manage.py which is settings your DJANGO_SETTINGS_MODULE to settings. It doesn't appear that you have a settings.py in your root directory, so this line in manage.py:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")
should become:
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Web-Interaction-APP.settings")
as you have in your wsgi.py file.

Deploying mysql database on a django app to production server [duplicate]

This question already has answers here:
No module named MySQLdb
(33 answers)
Closed 6 years ago.
I am trying to deploy a django app on a shared hosting. and i am currently facing issues deploying the app especially the mysql database. I have created the mysql database on the production server. But when trying to run manage.py collectstatic command i am getting the below attached error. I am assuming it has something to do with the mysqldb library.
I don't know how to go about this. Deploying this mysql database
Which ip address should i put on the host part of the db config in the settings.py file. Is it my production server ip address?
Please bear with me i am new to django especially the deployment part. Can't find enough info on the django deployment documentations.
This is the error i am getting on my production server:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
utility.execute()
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/core/management/__init__.py", line 327, in execute
django.setup()
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models(all_models)
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/opt/python27/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/contrib/auth/models.py", line 4, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/contrib/auth/base_user.py", line 49, in <module>
class AbstractBaseUser(models.Model):
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/db/models/base.py", line 108, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/db/models/base.py", line 307, in add_to_class
value.contribute_to_class(cls, name)
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/db/models/options.py", line 263, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/db/utils.py", line 212, in __getitem__
backend = load_backend(db['ENGINE'])
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/db/utils.py", line 116, in load_backend
return import_module('%s.base' % backend_name)
File "/opt/python27/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home4/techaven/mydjango/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 28, in <module>
raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb
This is a duplicate question. You have your answer in there >> No module named MySQLdb
As you see in your logs: Error loading MySQLdb module: No module named MySQLdb

No translation file found for domain 'django'

Environment :
python 2.7.10
django 1.8.6(copied from site-package folder)
I copied the django distribute into my project folder.
Thinking it will work well
It works perfectly in one of my machine, but throw an error below in another one
Traceback (most recent call last):
File "manage.py", line 12, in <module>
execute_from_command_line(sys.argv)
File "cmdb_lib/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "cmdb_lib/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "cmdb_lib/django/core/management/commands/test.py", line 30, in run_from_argv
super(Command, self).run_from_argv(argv)
File "cmdb_lib/django/core/management/base.py", line 393, in run_from_argv
self.execute(*args, **cmd_options)
File "cmdb_lib/django/core/management/commands/test.py", line 74, in execute
super(Command, self).execute(*args, **options)
File "cmdb_lib/django/core/management/base.py", line 458, in execute
translation.activate(saved_locale)
File "cmdb_lib/django/utils/translation/__init__.py", line 146, in activate
return _trans.activate(language)
File "cmdb_lib/django/utils/translation/trans_real.py", line 222, in activate
_active.value = translation(language)
File "cmdb_lib/django/utils/translation/trans_real.py", line 206, in translation
_translations[language] = DjangoTranslation(language)
File "cmdb_lib/django/utils/translation/trans_real.py", line 115, in __init__
self._init_translation_catalog()
File "cmdb_lib/django/utils/translation/trans_real.py", line 153, in _init_translation_catalog
translation = self._new_gnu_trans(localedir, use_null_fallback)
File "cmdb_lib/django/utils/translation/trans_real.py", line 136, in _new_gnu_trans
fallback=use_null_fallback)
File "/usr/local/lib/python2.7/gettext.py", line 472, in translation
raise IOError(ENOENT, 'No translation file found for domain', domain)
IOError: [Errno 2] No translation file found for domain: u'django'
But in my settings.py I set LANGUAGE_CODE = 'en-us' as default!
How could this throw an error?
I tried to pip install django, problem solved ,but I still want to include the django package in my project.
How can I achieve this?
I think I got the solution , my .gitignorefile ignored the .mo file , I added it and everything 's OK now
If you are using git, check your .gitignore file and comment*.mo
add the missing .mofile ,and things will be good

using the shell in Django

I am a newbie in Django un I want to write a Test for Django Web-poll application (https://docs.djangoproject.com/en/1.8/intro/tutorial01/)
but I got this error:
devuser#localhost:~/Django-apps/poolApp$ django-admin shell --plain --no-startup
Traceback (most recent call last):
File "/usr/local/bin/django-admin", line 9, in <module>
load_entry_point('Django==1.8.3', 'console_scripts', 'django-admin')()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/core/management/base.py", line 405, in run_from_argv
connections.close_all()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/utils.py", line 258, in close_all
for alias in self:
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/utils.py", line 252, in __iter__
return iter(self.databases)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/utils/functional.py", line 60, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/db/utils.py", line 151, in databases
self._databases = settings.DATABASES
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.3-py2.7.egg/django/conf/__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
I would like to know what is the best approach to do it:
define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure()
Use manage.py, not django-admin.
What does work is using
django-admin shell --plain --no-startup --pythonpath "." --settings "myproject.settings"
while you are in the root of your django app.
However manage.py shell (or the amazing shell_plus from django_extensions https://github.com/django-extensions/django-extensions) is recommended instead

Categories