使用django:from“python manage.py shell”到python脚本

编程入门 行业动态 更新时间:2024-10-26 00:31:18
本文介绍了使用django:from“python manage.py shell”到python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以移动到一个python项目目录(例如c:\www\myproject),然后发布

I can move to a python project directory (say c:\www\myproject) and then issue

python manage.py shell

然后我可以使用django项目中的所有模块,说下面的命令从shell命令:

and then I can use all modules from django project, say the following piece of commands from the shell command:

import settings from django.template import Template, Context t=Template("My name is {myname}.") c=Context({"myname":"John"}) f = open('write_test.txt', 'w') f.write(t.render(c)) f.close

now当我尝试将所有命令收集到一个python脚本中时,说mytest.py,我无法执行脚本。我必须错过一些重要的东西。

now, when I tried to collect all my commands into a python script, say "mytest.py", I cannot execute the script. I must missed something important.

我发出了python mytest.py

I issued python mytest.py

然后我得到了导入错误:无法导入设置是否在sys路径上?

then I got Import error: could not import settings Is it on sys path?"

我位于settings.py所在的项目目录中。

I'm in the project directory where settings.py resides.....

有些人可以帮助我吗?

谢谢。

推荐答案

尝试使用 Django管理命令。

# 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

更多推荐

使用django:from“python manage.py shell”到python脚本

本文发布于:2023-10-22 22:21:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518899.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   python   django   manage   shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!