23π
Try using a Django management command instead.
# myproject/myapp/management/commands/my_command.py
from django.core.management.base import NoArgsCommand
from django.template import Template, Context
from django.conf import settings
class Command(NoArgsCommand):
def handle_noargs(self, **options):
t=Template("My name is {myname}.")
c=Context({"myname":"John"})
f = open('write_test.txt', 'w')
f.write(t.render(c))
f.close
And then (if you follow the docs) you will be able to execute the command in the following fashion:
python manage.py my_command
23π
This method is deprecated in Django 1.4. Use django.conf.settings.configure()
instead
(see @adiewβs answer for example code).
Old method follows.
Put this at the beginning of your script
from django.core.management import setup_environ
import settings
setup_environ(settings)
This is really what the manage.py does behind the scene. To see it view the Django source in django/core/management/__init__.py
. After executing these lines everything should be just like in ./manage.py shell
.
- [Django]-Why won't Django use IPython?
- [Django]-How do I subtract two dates in Django/Python?
- [Django]-Using IntellijIdea within an existing virtualenv
11π
Try put these two lines at the beginning of your script:
from django.conf import settings
settings.configure() # check django source for more detail
# now you can import other django modules
from django.template import Template, Context
- [Django]-How to create table during Django tests with managed = False
- [Django]-Django "login() takes exactly 1 argument (2 given)" error
- [Django]-Python regex for integer?
- [Django]-CSS styling in Django forms
- [Django]-Django 1.3.1 compilemessages. Error: sh: msgfmt: command not found
- [Django]-Django project base template
- [Django]-Django and Restful APIs
- [Django]-How can I allow django admin to set a field to NULL?
- [Django]-Putting a django login form on every page
2π
Instead of manually adding things to your python script, or having to fit in the management command format, in case this is not something that needs to stay around long, you can get all the benefits of the Django environment by running your script with ./manage.py runscript <myscript.py>
β¦ but if your script is in your project folder, then you can just add this line to the top of the python script: import os; os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
- [Django]-__init__() got an unexpected keyword argument 'mimetype'
- [Django]-Django Rest Framework Postman Token Authentication
- [Django]-Heroku & Django: "OSError: No such file or directory: '/app/{myappname}/static'"
1π
Saw in https://stackoverflow.com/a/24456404/4200284 a good solution for Django >= 1.7 and in case someone uses django-configurations this worked for me:
import sys, os, django
sys.path.append('/path/to/project')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.local") # path to config
## if using django-configurations
os.environ.setdefault("DJANGO_CONFIGURATION", "Local")
from configurations import importer
importer.install()
django.setup() ## apparently important for django 1.7
from foo.models import Bar
print Bar.objects.all()
- [Django]-In Django filter statement what's the difference between __exact and equal sign (=)?
- [Django]-Accessing request headers on django/python
- [Django]-How to start doing TDD in a django project?
0π
Here is yet another variant: I wrote and often use a management command βrunβ which has the advantage that scripts can see their command-line parameters:
http://lino-framework.org/api/lino.management.commands.run.html
- [Django]-How to Check if request.GET var is None?
- [Django]-Class views in Django
- [Django]-Sending HTML email in django