如何在django shell中重新加载模块? [英] How to reload modules in django shell?

查看:689
本文介绍了如何在django shell中重新加载模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在和Django一起工作,一直使用Django shell。令人讨厌的部分是Django服务器重新加载代码更改时,shell没有,所以每次更改我正在测试的方法时,我需要退出shell并重新启动它,重新导入所有模块需要,重新初始化我需要的所有变量。虽然iPython历史记录保存了很多打字,这仍然是一个痛苦。有没有办法使django shell自动重新加载,与django开发服务器相同的方式?

I am working with Django and use Django shell all the time. The annoying part is that while the Django server reloads on code changes, the shell does not, so every time I make a change to a method I am testing, I need to quit the shell and restart it, re-import all the modules I need, reinitialize all the variables I need etc. While iPython history saves a lot of typing on this, this is still a pain. Is there a way to make django shell auto-reload, the same way django development server does?

我知道reload(),但是我导入了很多模型,通常使用app.models import * 语法中的,所以reload()没有太多的帮助。

I know about reload(), but I import a lot of models and generally use from app.models import * syntax, so reload() is not much help.

推荐答案

我建议使用dongweiming所述的django-extensions项目。但是,而不是只使用'shell_plus'管理命令,请使用:

I recommend using the django-extensions project like stated above by dongweiming. But instead of just 'shell_plus' management command, use:

manage.py shell_plus --notebook

这将在您的网络浏览器上打开一个IPython笔记本。将代码写入单元格,导入等并运行。

This will open a IPython notebook on your web browser. Write your code there in a cell, your imports etc. and run it.

当您更改模块时,只需单击笔记本菜单项内核 - >重新启动

When you change your modules, just click the notebook menu item 'Kernel->Restart'

在那里去,你的代码正在使用你修改的模块。

There you go, your code is now using your modified modules.

这篇关于如何在django shell中重新加载模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆