Modifing settings in Django - django

I'm trying to get Django's manage.py to run with modified settings files.
I have three settings files
settings.py
preview.py
live.py
settings.py contains my base settings but on live & preview sites I point the ['DJANGO_SETTINGS_MODULE'] to live or preview which in turn load the base file and any settings specific to that environment.
This works fine, until I try to run migrations using manage.py
I updated the manage.py file to import settings.preview but now when I try to run syncdb or migrate I get this error:
django.core.exceptions.ImproperlyConfigured:
You haven't set the DATABASE_ENGINE
setting yet.
My settings file has this in it though:
DATABASE_ENGINE = 'mysql'
How can I get this to work?

Don't modify manage.py if you can help it. Instead pass it the --settings argument to choose an alternate settings module. Setting up a shell script or alias will make it easier to use this.

Related

[django]how can i change Settings.py?

Several configuration files exist.
If these files have different names
How do I change the settings file every time I run this command?
"python manage.py runserver"
Its so simple
read Main Django Tutorial, its all about setting django configuration
a shortcut for using in runserver command is --settings= and this also works with uwsgi
but if you intend to change setting without re-running the server django-constance is the answer
you can add to manage.py file
def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project_name.settings_name')
After that run
py manage.py ----

Pycharm runserver error with custom settings.py files

just started using pycharm on a existing project, and having difficulty using the run/debug configurations in order to run the test server. The issue stems from their being no settings.py module as standard in django projects. Instead there is settings_base.py and then two different settings modules (settings_live.py and settings_test.py) that extend it by using 'from settings_base import *'. This causes manage.py to fail when running runserver because it can't find settings.py.
In the Django Support settings I have set the project root, and set Settings to settings_test.py however this has not helped. In the Django server run configuration I have setup I also have...
DJANGO_SETTINGS_MODULE=settings_test
... in the Environment Variables section. However when I use runserver it still says
Error: Can't find the file 'settings.py' in the directory containing '/home/pete/Projects/the_project/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 tried wrapping the entire settings_test.py module in a try/except to see if it really was an import error, however it did not seem to work. Is there something I am missing?
Maybe you should try adding --settings=settings_test to the "Additional options" in your PyCharm Run configuration and make sure that "Working directory" points to correct path.

Django can't find settings

I have a Django project that contains a "config" folder, which has a settings_dev.py file inside of it which is my settings file.
If I try doing anything with manage.py, it complains that it can't find my settings file, even if I explicitly provide it via the --settings option, e.g.
python manage.py syncdb --settings=config.settings_dev
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.
However, if I rename my "config" folder to "settings", it works fine. e.g.
python manage.py syncdb --settings=settings.settings_dev
works fine.
What else do I need to specify for it to know that my settings folder is actually named config?
Look into manage.py. It tries to import settings not conf. You have to modify your manage.py file
create a generic settings.py in the project folder that points to the config module. don't forget the __init__.py in the config folder. This might be better than modifying manage.py.
#!/usr/bin/env python
from django.core.management import setup_environ
try:
import config.settings as settings
except ImportError:
import sys
sys.stderr.write("Couldn't find the settings.py module.")
sys.exit(1)
setup_environ(settings)
Have a look at this answer:
This error occurs as well if there is an import error within settings.py.
https://stackoverflow.com/questions/6036599/bizarre-error-importing-settings-in-django#=

Install django on webfaction

I am trying to install django on Webfaction, and i have one all the steps like in the tutorial here: http://wiki.osqa.net/display/docs/Installing+OSQA+on+WebFaction
But when i am trying to synchronyse the database, if i enter python2.5 manage.py syncdb --all i get: Usage: manage.py syncdb [options]
Create the database tables for all apps in INSTALLED_APPS whose tables haven't already been created.
manage.py: error: no such option: --all
if i enter manage.py syncdb i get Error: Could not import settings 'osqa.settings' (Is it on sys.path? Does it have syntax errors?): No module named settings
i have set all like in the tutorial...is a problem with the import line in settings.py : from settings_local import * as if i comment that i get an error from if DEBUG line... Where am i wrong??? all the files have the same rights - 617 and i cannot change the rights, also i don;t think is a problem with rights. thanks!
Try enabling DEBUG in the settings file to see if you get any further indication of the failure. There is a path problem somewhere. You could also try running python in the osqa directory and enter import settings_local to see what you get.

Unable to start a django server in my computer

I exported the path of my django project by
$ export DJANGO_SETTINGS_MODULE=/Users/masi/Documents/Test/djangobook/ch3.settings
I run unsuccessfully
$ django-admin.py runserver
Error: Could not import settings '/Users/masi/Documents/Test/djangobook/ch3.settings' (Is it on sys.path? Does it have syntax errors?): Import by filename is not supported.
How can you start Django server without the error message?
Your $DJANGO_SETTINGS_MODULE should just be set to ch3.settings. Just make sure that the ch3 app is in your $PYTHONPATH, too.
For example, if your app is at /Users/masi/Documents/Test/djangobook/, then set $DJANGO_SETTINGS_MODULE to ch3.settings, and make sure your $PYTHONPATH includes /Users/masi/Documents/Test/djangobook.
$ export PYTHONPATH=/Users/masi/Documents/Test/djangobook/
$ export DJANGO_SETTINGS_MODULE=ch3.settings
From the django docs on django-admin.py and manage.py:
django-admin.py is Django’s command-line utility for administrative tasks.
In addition, manage.py is automatically created in each Django project. manage.py is a thin wrapper around django-admin.py that takes care of two things for you before delegating to django-admin.py:
It puts your project’s package on sys.path.
It sets the DJANGO_SETTINGS_MODULE environment variable so that it points to your project’s settings.py file.
Generally, when working on a single Django project, it’s easier to use manage.py
So, if your directory structure looks like:
djangobook/
ch3/
settings.py
Do the following and you can ignore all DJANGO environment variables (unless you have some really weird install):
$ cd /Users/masi/Documents/Test/djangobook/ch3/
$ python manage.py runserver
For those that come across the same error, when trying to run something similar:
python manage.py runserver --settings=settings_dev
When the settings file is within an app directory, like so:
mysite/
settings.py
settings_dev.py
requirements.txt
manage.py
You don't have to specify $PYTHONPATH (at least not four years on) you just need to make sure your --settings value contains the folder name — you also need to use dot notation, slashes will not do.
python manage.py runserver --settings=mysite.settings_dev
It is the same story when exporting a $DJANGO_SETTINGS_MODULE value:
export DJANGO_SETTINGS_MODULE=mysite.settings_dev
Might save someone else the time that I lost working that out.
You can also try manage.py.
From your project directory, run
$ python manage.py runserver
Even though it's just a wrapper, manage.py always works for me while django-admin.py doesn't. Obviously we're both doing something wrong (I just got started with Django), but this should get you going at least.