tags:

views:

339

answers:

3

Every time I log on to my server through SSH I need to type the following:

export DJANGO_SETTINGS_MODULE=settings

if I do not any usage of the manage.py module fails

My manage.py has the following added code:

if "notification" in settings.INSTALLED_APPS:
    from notification import models as notification

    def create_notice_types(app, created_models, verbosity, **kwargs):
        notification.create_notice_type("friends_invite", _("Invitation Received"), _("you have received an invitation"))
        notification.create_notice_type("friends_accept", _("Acceptance Received"), _("an invitation you sent has been accepted"))

    signals.post_syncdb.connect(create_notice_types, sender=notification)
else:
    print "Skipping creation of NoticeTypes as notification app not found"

Any ideas?

+4  A: 

Yourmanage.py is referencing an application (notifications). This forces Django to complain about DJANGO_SETTINGS_MODULE being set because the Django environment hasn't been set up yet.

Incidentally, you can force the enviroment setup manually, but honestly I wouldn't do this in manage.py. That's not really a good practice in my opinion.

Here is how you can manually setup the Django environment from within any app (or program for that matter):

# set up the environment using the settings module
from django.core.management import setup_environ
from myapp import settings
setup_environ(settings)
jathanism
Thank you for your great reply.You mean that it's not recommended because reduces portability?Please explain why.
RadiantHex
Yes, it reduces portability. Anything that couples the management tool to an application should be avoided. Applications and projects should all have independently moving parts. Imagine a future where you need to move an application to another project, or remove one from a current project. It's best to not have them intertwine and instead perform any sort of checks (safely) from within the apps themselves.
jathanism
+3  A: 

You need to set the DJANGO_SETTINGS_MODULE environment variable because it's how Django knows what your settings module is called (so you can have different ones per project or for testing and development.) You can set it in the scripts themselves before you import django (directly or indirectly) but that won't do much good when you run the Django-provided scripts.

The easiest solution is probably to just set DJANGO_SETTINGS_MODULE in your shell's startup scripts, so you won't have to set it manually anymore. The usual files to add it to are .bash_profile and .bashrc (if you do indeed use bash.)

Thomas Wouters
Thanks Thomas. Can you point in me in some direction regarding the bash scripts? I'm logging in via Putty and have no clue when the scripts would kick in.
RadiantHex
Well, the documentation for whatever OS or distribution you are logging into would be a good start :) But a good bet is just adding the same export line as you were already using into your .bash_profile or .bashrc file, somewhere near other similar export lines. Either one of them should be loaded for every new shell.
Thomas Wouters
A: 

By default, manage.py looks for a settings module in the same directory as itself. If it doesn't find one, it bombs out with a message to use django-admin.py instead. It doesn't actually set up the environemnt until it runs execute_manager. If you need to run your hooks before calling your management functions, the practice I've seen suggested is to put them in the relevant app's models.py.

jcdyer