Using a settings file other than settings.py in Django - django

I want to use a different settings file in django -- specifically settings_prod -- yet whenever I try to do a syncdb with --settings=settings_prod, it complains:
python2.6 manage.py syncdb --settings=settings_prod
Error: Can't find the file 'settings.py' in the directory containing 'manage.py'. It appears you've customized things.
You'll have to run django-admin.py, passing it your settings module.
(If the file settings.py does indeed exist, it's causing an ImportError somehow.)
I've also tried setting the environment variable DJANGO_SETTINGS_MODULE=settings_prod to no end.
Edit: I have also set the environment variable in my wsgi file, also to no end:
import os
import sys
from django.core.handlers.wsgi import WSGIHandler
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings_prod'
application = WSGIHandler()
Suggestions?

Try creating a settings module.
Make a settings folder in the same directory as manage.py.
Put your different settings files in that folder (e.g. base.py and prod.py).
Make __init__.py and import whatever settings you want to use as your default. For example, your __init__.py file might look like this:
from base import *
Run your project and override the settings:
$ python2.6 manage.py syncdb --settings=settings.prod

I do know that no matter what you do with manage.py, you're going to get that error because manage.py does a relative import of settings:
try:
import settings # Assumed to be in the same directory.
http://docs.djangoproject.com/en/dev/ref/django-admin/#django-admin-option---settings
Note that this option is unnecessary
in manage.py, because it uses
settings.py from the current project
by default.
You should try django-admin.py syncdb --settings=mysettings instead

this works for me:
DJANGO_SETTINGS_MODULE=config.settings.abc python manage.py migrate

this will help you:
create a another file "setting_prod.py" with your original settings.py file.
write down your setting which you need to run, in setting_prod.py file.
Then import setting_prod.py file in your settings.py file.
for ex.
settings.py:
VARIABLE = 1
import setting_prod
setting_prod.py
VARIABLE = 2
After importing setting_prod.py file in settings.py file, VARIABLE will set to new value to "2" from "1".

We can use this method to set different settings file, for example, I use different settings file for my unit test (settings_unit_test.py). Also I do have other settings file for different infrastructure environment settings_dev.py, settings_test.py and settings_prod.py.
In windows environment(same can done in linux as well)
set DJANGO_SETTINGS_MODULE=settings_unit_test
set PYTHONPATH=<path_of_your_directory_where_this_file_'settings_unit_test.py'_is_kept>

Related

Django 1.8 import issue

This is the first time I need to run my app deeper in the directory structure, I have obviously messed up my app structure while setting this up and can't get it right anymore. Could somebody help me out here?
In my start.sh file I define following env vars:
export PYTHONPATH=${PYTHONPATH}:'/home/ubuntu/workspace/skw/3rd_apps/'
export PYTHONPATH=${PYTHONPATH}:'/home/ubuntu/workspace/skw/cskw/'
And my dir structure looks like this:
/home/ubuntu/workspace/skw/start.sh (also manage.py and gulpfile)
/home/ubuntu/workspace/skw/3rd_apps/ (django is here)
/home/ubuntu/workspace/skw/cskw/ (contains __init__.py)
/home/ubuntu/workspace/skw/cskw/apps/ (contains __init__.py)
/home/ubuntu/workspace/skw/cskw/core_app/ (settings.py is here and __init__.py)
I cycle between two errors here:
1. When trying to setup a custom signup form:
ACCOUNT_SIGNUP_FORM_CLASS = 'core_app.forms.SignupForm'
The form is in place but i get this error:
django.core.exceptions.ImproperlyConfigured: Module "core_app.forms" does
not define a "SignupForm" class
And if I fix this I can not import anything from apps, it expects them to be in core_app and does not see the app module at the same level as core_app.
manage.py points to "core_app.settings", so do the variables in settings.py.

Django: How to specify path to settings file

I know this question has already been asked multiple times but I still can't seem to find a solution that works for me. My Django project folder looks like this:
my_project
__init__.py
manage.py
my_first_app
my_second_app
core
Now the "core" folder looks like this:
__init__.py
some_other_stuff.py
settings
__init__.py
prod.py
dev.py
local.py -> dev.py
local.py is a symbolic link pointing to the right settings file, dev.py on my machine, prod.py in production.
Here's my problem: when I try to use manage.py, I get a weird error ImproperlyConfigured: The SECRET_KEY setting must not be empty. When I pass the path to the settings file local.py as an argument (--settings=core.settings.local) it runs fine. I figured the problem was that Django didn't know where to look for the settings file. How can I tell him (her?) where to look?
I already tried exporting the path to the env (export DJANGO_SETTINGS_MODULE=core.settings.local) and setting the PYTHONPATH to the parent directory, to no avail.
The primary use of __init__.py is to initialize Python packages. The easiest way to demonstrate this is to take a look at the structure of a standard Python module.
package/
__init__.py
file1.py
file2.py
As you can see in the structure above the inclusion of the __init__.py file in a directory indicates to the Python interpreter that the directory should be treated like a Python package
__init__.py can be an empty file but it is often used to perform setup needed for the package(import things, load things into path, etc).
One common thing to do in your __init__.py is to import selected Classes, functions, etc into the package level so they can be convieniently imported from the package.
In our example above we can say that file.py has the Class File. So without anything in our __init__.py you would import with this syntax:
from package.file import File
However you can import File into your __init__.py to make it available at the package level:
# in your __init__.py
from file1 import File
# now import File from package
from package import File
Source
So for conclusion, when you call import in __init__.py in a package mypackage, it's like you use package as a simple python file, that's what my solution do:
from .local import * in __init__.py
I haven't use this before in the settings case but I use it when I wanna to subdivide my models in a Django app, models.py --> models package, ./manage syndb doesn't discover my models declared, I found so this solution that's similar. You can find more details here
Last thing, I'm sure there's others solution to your problem, but this can be the most simple.
Good luck
You are in import hell somewhere. Had this problem too one time. The only way to find out where the root of your problem is, might be to disable all apps, try starting the server, enable the first app, start the server, enable the next etc.
BTW: your project layout should not be used from Django 1.4 onward. https://docs.djangoproject.com/en/dev/releases/1.4/#updated-default-project-layout-and-manage-py
I'd try to use the new layout and hope that it 'just works'.
I think you need to change the name of the file that the manage.py is looking for.
try:
imp.find_module('settings') # Assumed to be in the same directory.
except ImportError:
import sys
sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n" % __file__)
sys.exit(1)
If you had the settings.py file in the same directory simple changing the 'settings' to 'local' would have worked.
But, since you have it in a different directory, I think you need to configure the settings. Refer to this: https://docs.djangoproject.com/en/dev/topics/settings/#using-settings-without-setting-django-settings-module
from django.conf import settings
settings.configure(DEBUG=True, TEMPLATE_DEBUG=True,
TEMPLATE_DIRS=('/home/web-apps/myapp', '/home/web-apps/base'))
Hope that helps.

Whats is the best way to migrate folder and files structure from django1.3 to django1.4?

I have a little project created with django1.3 and I want to migrate it to django1.4 but since the files structure change a little, what is the best way to migrate?
Read https://docs.djangoproject.com/en/dev/releases/1.4/ first.
For a quick run, just update env from Django1.3 to 1.4, tweak settings file and project code by fixing any incompatibility warning and imports issue.
For a clean update, better to create an empty project w/ the same name of the current project and migrate it w/ current code, mainly override foo/settings.py and foo/urls.py .
I prefer to follow settings structure by http://justcramer.com/2011/01/13/settings-in-django/ , when it's done there is no need to merge base settings.py each time you update Django version.
Regarding directory structure, I think all you have to do is move your manage.py one level up and change it's contents to this(replacing {{project_name}} with the name of your project):
#!/usr/bin/env python
import os, sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{{ project_name }}.settings")
from django.core.management import execute_from_command_line
execute_from_command_line(sys.argv)
Look here for details: https://docs.djangoproject.com/en/dev/releases/1.4/#updated-default-project-layout-and-manage-py

Django sys.path.append for project *and* app needed under WSGI

Could somebody give me a pointer on why I need to add my project root path to the python path as well as the application itself in my WSGI file?
Project base is called 'djapp', the application is called 'myapp'.
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..')
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../djapp')
os.environ['DJANGO_SETTINGS_MODULE'] = 'djapp.settings'
If I omit the line with "/../djapp/" the log tells my that 'myapp' can not be imported, even though 'djapp.settings' is. (validating 'djapp' was imported)
It al runs properly with the ./manage.py command. there's a __init__ in the project folder.
For testings sake, I see the same issue using addsitedir:
site.addsitedir('/home/user/web/project/')
site.addsitedir('/home/user/web/project/djapp')
Since djapp (the django project folder) is in a parent folder that also belongs to the deployment I renamed the djapp folder simply to project.
Then this code is always correct:
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..' )
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../project')
os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
The complete folder layout being:
host.example.com\
etc\
bin\
project\
logs\
And what have you. This way project can always be called project :)
Hope that helps.
GrtzG
Presumably you've got code within your project which is doing from myapp import foo.
Two options:
change that to from djapp.myapp import foo, which is not recommended as it prevents portability;
only add djapp in your WSGI, and set the DJANGO_SETTINGS_MODULE to just 'settings'.

Problem importing files in Django settings.py

I have a Django project which seemed to work pretty well with settings.py, which also imported a local_settings.py without problem.
I've now added the following lines at the end of the settings file :
try:
from extras import *
except ImportError, e:
print "import extras failed :: " + `e`
extras.py is a file of extra configuration information sitting in the same directory as settings.py and local_settings.py, however, I'm now getting :
import extras failed :: ImportError('Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.',)
This seems to be due to me trying to
from django.contrib.auth.models import User,UserManager
from django.db import models
in that extras.py file.
Anyone have any ideas?
cheers
Typically, having a line like
from django.db import models
in settings.py will lead to a circular dependency. This causes an import error, which gets reported slightly differently in different versions of Django. For example, if I add that line to a working Django setup and invoke "manage.py shell", I get:
Error: Can't find the file
'settings.py' in the directory
containing './manage.py'. It appears
you've customized things. You'll have
to run django-admin.py, passing it
your settings module. (If the file
settings.py does indeed exist, it's
causing an ImportError somehow.)
If I remove that line, everything's fine again.
The reason is that Django's model loading machinery (located in the django.db.models package) imports settings.py, reads its INSTALLED_APPS to see what apps should be installed, and then loads those apps. (You can confirm this by adding a print statement to __init__.py for one of your installed apps.)
If you try to import django.db stuff in settings.py, that will lead to a circular import dependency and an ImportError-related error message.
One workaround is to move the functionality which requires the problematic imports (and the imports themselves) to an app.
So how is DJANGO_SETTINGS_MODULE set in your environment? It could be either that, or the result of the implicit import of settings which your other nested imports are causing while settings is being imported, a "circular dependency" that can have several nasty effects (though I don't believe it would have the specific one you're observing, so I lean towards the first hypothesis).