Sometimes I want to execute a file in the context of my Django project, just as if I were using the shell, but with the convenience of using a text editor. This is mainly to try something out, or quickly prototype some functionality before putting it into a view, test, recurring task, or management command.
有时我想在我的Django项目的上下文中执行一个文件,就像我使用shell一样,但是使用文本编辑器的便利性。这主要是为了尝试一些东西,或者在将其放入视图,测试,重复任务或管理命令之前快速构建一些功能原型。
I know I can stick these lines at the top of my .py file and it'll run in the Django context:
我知道我可以将这些行放在我的.py文件的顶部,它将在Django上下文中运行:
import sys
sys.path.append('/location/of/projet')
from django.core.management import setup_environ
import settings
setup_environ(settings)
I thought it'd be easier to make a management command that takes on argument, a python module to run, and executes it in the Django environment. Here's the 'runmodule' command I wrote:
我认为制作一个带有参数的管理命令,运行python模块并在Django环境中执行它会更容易。这是我写的'runmodule'命令:
from django.core.management.base import BaseCommand, CommandError
class Command(BaseCommand):
help = "Runs an arbitrary module, in the Django environment, for quick prototyping of code that's too big for the shell."
def handle(self, *args, **options):
if not args:
return
module_name = args[0]
try:
__import__(module_name)
except ImportError:
print("Unable to import module %s. Check that is within Django's PYTHONPATH" % (module_name))
This looks like it works -- I can stick some code in a module, and pass it as an argument to this command, and it'll get executed, e.g.
这看起来很有效 - 我可以在模块中粘贴一些代码,并将其作为参数传递给此命令,并且它将被执行,例如,
python manage.py runmodule myapp.trysomethingout
which will execute myapp/trysomethingout.py. Is this the best way to do this?
这将执行myapp / trysomethingout.py。这是最好的方法吗?
1 个解决方案
#1
9
The best way to execute a script with the correct django context is to set the DJANGO_SETTINGS_MODULE
environment variable to your settings module (and appropriate PYTHONPATH
if needed). In windows this usually means executing:
使用正确的django上下文执行脚本的最佳方法是将DJANGO_SETTINGS_MODULE环境变量设置为您的设置模块(如果需要,还可以设置适当的PYTHONPATH)。在Windows中,这通常意味着执行:
set DJANGO_SETTINGS_MODULE=setting
and in bash :
在bash中:
export DJANGO_SETTINGS_MODULE=setting
You can now import your models etc.
您现在可以导入模型等。
see: https://docs.djangoproject.com/en/dev/topics/settings/#designating-the-settings .
请参阅:https://docs.djangoproject.com/en/dev/topics/settings/#designating-the-settings。
Note that in order to import the settings
module, one should use from django.conf import settings
. This takes into account the DJANGO_SETTINGS_MODULE
instead of automatically using settings.py .
请注意,为了导入设置模块,应该使用django.conf导入设置。这考虑了DJANGO_SETTINGS_MODULE而不是自动使用settings.py。
#1
9
The best way to execute a script with the correct django context is to set the DJANGO_SETTINGS_MODULE
environment variable to your settings module (and appropriate PYTHONPATH
if needed). In windows this usually means executing:
使用正确的django上下文执行脚本的最佳方法是将DJANGO_SETTINGS_MODULE环境变量设置为您的设置模块(如果需要,还可以设置适当的PYTHONPATH)。在Windows中,这通常意味着执行:
set DJANGO_SETTINGS_MODULE=setting
and in bash :
在bash中:
export DJANGO_SETTINGS_MODULE=setting
You can now import your models etc.
您现在可以导入模型等。
see: https://docs.djangoproject.com/en/dev/topics/settings/#designating-the-settings .
请参阅:https://docs.djangoproject.com/en/dev/topics/settings/#designating-the-settings。
Note that in order to import the settings
module, one should use from django.conf import settings
. This takes into account the DJANGO_SETTINGS_MODULE
instead of automatically using settings.py .
请注意,为了导入设置模块,应该使用django.conf导入设置。这考虑了DJANGO_SETTINGS_MODULE而不是自动使用settings.py。