Django snippets: Executing standalone scripts
febrero 22, 2012 Deja un comentario
Sometimes it’s necessary run some background or cron tasks in a Django app. For example, you can recollect and email you some stats, purge temporal files, process images, etc.
In these cases, you need access to Django framework but you don’t use neither mod_python, wsgi or manage.py to run it.
I have the following simple snippet for it that works fine in my apps.
#!/usr/bin/env python import os import sys if __name__ == '__main__': # Setup environ os.environ['DJANGO_SETTINGS_MODULE'] = "project_name.settings" sys.path.append(<path_to_project_base_dir>) # Bellow this line, welcome to Django World # Now you can do things like "from project_name.app_name import models"
If you want to depth in this topic, you must visit Standalone Django scripts