I've made a standalone program that uses Django modules.

At settings.py:
import os
PROJECT_PATH = os.path.dirname(os.path.abspath(__file__))

At standalone script:
import sys, os
try:
    import settings # this file was in the same directory of
settings.py
    sys.path.append(os.path.join(settings.PROJECT_PATH, '..'))
    os.environ['DJANGO_SETTINGS_MODULE'] = 'project_name.settings'
    from django.contrib.flatpages.models import FlatPage
    from django.contrib.sites.models import Site
    # .. import other necessary modules
except ImportError:
    sys.stderr.write('Error: Cannot initialize environment.\n')
    sys.exit(1)

After this code, I could import and use Django modules and my custom
models as in the views.

On Sep 6, 6:35 am, mamcxyz <[EMAIL PROTECTED]> wrote:
> I wanna debug inside a IDE (komodo 4) my test cases for django, so I'm
> looking for a way to simply run the script from the IDE and step in as
> usual.
>
> This is my layout:
>
> F:\Proyectos\Python\jhonWeb <= root inside it:
>
> \demo\ THE SITE with the settings file
> \shared\ shared models I have develop to share across several projects
> as blog, tags and users
>
> Inside \shared\ have:
>
> \articles\tests.py
>
> I try this:
>
> import sys
> import os
>
> project_dir = os.path.abspath('../../') # Go to F:\Proyectos\Python
> \jhonWeb
> sys.path.append(project_dir) # Add the root to the path and
> sys.path.append(project_dir+r'\\demo') #add the test project
> os.environ['DJANGO_SETTINGS_MODULE'] = 'demo.settings'
>
> But I have:
>
> "D:\Programacion\Python\Python24\lib\site-packages\django\db\models
> \base.py", line 52, in __new__
>     new_class._meta.app_label = model_module.__name__.split('.')[-2]
> IndexError: list index out of range
>
> Obviously the thing work as usual if run it from python manage.py test
> and similars.
>
> Also, why is necesary the DJANGO_SETTINGS_MODULE magic after all???
>
> NOTE: Current trunk revision 6050.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to