4๐
I created a shell script with the following:
/path/to/ipython
qtconsole --pylab inline -c "run /path/to/my/site/shell.py"
You only need the --pylab inline
part if you want the cool inline matplotlib graphs.
And I created a python script shell.py
in /path/to/my/site
with:
import os
working_dir = os.path.dirname(__file__)
os.chdir(working_dir)
import settings
import django.core.management
django.core.management.setup_environ(settings)
Running my shell script gets me an ipython qtconsole with the benefits of the django shell.
7๐
The docs here say:
If youโd rather not use manage.py, no problem. Just set the
DJANGO_SETTINGS_MODULE environment variable to mysite.settings and run
python from the same directory manage.py is in (or ensure that
directory is on the Python path, so that import mysite works).
So it should be enough to set that environment variable and then run ipython qtconsole
. You could make a simple script to do this for you automatically.
- Django KeyError: "'__name__' not in globals"
- Overwriting a block within an `include`d template from an extended template
2๐
You can check the code that runs the shell here. Youโll see that there is no where to configure what shell is run.
What you could do is copy this file, rename it as shell_qt.py and place it in your own projectโs management/commands directory. Change it to run the QT console and then you can run manage.py shell_qt
.
- Store browser tab specific data
- Django-registration, template
- Where do I register an rq-scheduler job in a Django app?
1๐
Since Django version 1.4, usage of django.core.management.setup_environ()
is deprecated. A solution that works for both the IPython notebook and the QTconsole is this (just execute this from within your Django project directory):
In [1]: from django.conf import settings
In [2]: from mydjangoproject.settings import DATABASES as MYDATABASES
In [3]: settings.configure(DATABASES=MYDATABASES)
Update: If you work with Django 1.7, you additionally need to execute the following:
In [4]: import django; django.setup()
Using django.conf.settings.configure()
, you specify the database settings of your project and then you can access all your models in the usual way.
If you want to automate these imports, you can e.g. create an IPython profile by running:
ipython profile create mydjangoproject
Each profile contains a directory called startup
. You can put arbitrary Python scripts in there and they will be executed just after IPython has started. In this example, you find it under
~/.ipython/profile_<mydjangoproject>/startup/
Just put a script in there which contains the code shown above, probably enclosed by a try..except
clause to handle ImportError
s. You can then start IPython with the given profile like this:
ipython qtconsole --profile=mydjangoproject
or
ipython notebook --profile=mydjangoproject
- How to access RequestContext in class-based generic views?
- How to use forms in django-cms?
- How to concatenate two model fields in a Django QuerySet?
- Web Application (Django) typical project folder structure
0๐
I also wanted to open the Django shell in qtconsole. Looking inside manage.py solve the problem for me:
Launch IPython qtconsole, cd to the project base directory and run:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
Dont forget to change โmyprojectโ to your project name.
- Django render_to_string() ignores {% csrf_token %}
- Django: correctly retrieve data where date and time are greater than now
0๐
You can create a command that extends the base shell command and imports the IPythonQtConsoleApp like so:
create file qtshell.py in yourapp/management/commands with:
from django.core.management.commands import shell
class Command(shell.Command):
def _ipython(self):
"""Start IPython Qt console"""
from IPython.qt.console.qtconsoleapp import IPythonQtConsoleApp
app = IPythonQtConsoleApp.instance()
app.initialize(argv=[])
app.start()
then just use python manage.py qtshell
- Google Oauth2.0 web application's "Authorized redirect URIs" must end with a public top-level domain (such as .com or .org)?
- How do I serve media files in a local Django environment?
- Django admin.TabularInline auto insert three blank row
0๐
A somewhat undocumented feature of shell_plus
is the ability to run it in โkernel only modeโ. This allows us to connect to it from another shell, such as one running qtconsole.
For example, in one shell do:
django-admin shell_plus --kernel
# or == ./manage.py shell_plus --kernel
This will print out something like:
# Shell Plus imports ... ... To connect another client to this kernel, use: --existing kernel-23600.json
Then, in another shell run:
ipython qtconsole --existing kernel-23600.json
This should now open a QtConsole. One other tip, instead of running another shell, you can also hit Ctrl+Z, and run bg
to tell current process to run in background.
- How to test coverage properly with Django + Nose
- Python-social-auth with Django: ImportError: No module named 'social_django'
-1๐
You can install django extensions and then run
python manage.py shell_plus --ipython
- Python easy_install fails with SSL certificate error for all packages
- TypeError at /en/ Object of type '__proxy__' is not JSON serializable