linux 利用 miniconda 搭建 jupyter notebook 开发环境(二)--Django

本篇接着上篇文章介绍如何使用 jupyter 进行 django 程序的调试和开发

安装django

#在 virtual python36 中安装 django
(base) ubuntu@ubuntu:~/my_python$ pip install django==1.11.13

新建 django project

(base) ubuntu@ubuntu:~/my_python$ django-admin startproject application

新建 app ,顺便做 migrate,创建一个superuser

(base) ubuntu@ubuntu:~/my_python/application$ python manage.py startapp web
(base) ubuntu@ubuntu:~/my_python/application$ python manage.py migrate
(base) ubuntu@ubuntu:~/my_python/application$ python manage.py createsuperuser --username admin

安装 django-extensions

(base) ubuntu@ubuntu:~/my_python/application$ pip install django-extensions

settings.py 添加 django_extensions 及 NOTEBOOK_ARGUMENTS

INSTALLED_APPS += ['django_extensions']

NOTEBOOK_ARGUMENTS = [
    '--ip', '0.0.0.0',
    '--port', '8889',
]

启动

(base) ubuntu@ubuntu:~/my_python/jianda$ python manage.py shell_plus --notebook

这就进入项目目录了(对原生目录稍有改动),一般用 Django Shell-Plus 新建文件,如图


image.png

进入 jupyter_notebook 新建文件 demo_1.ipynb

在开头运行如下代码引入 django settings

import os, sys
MYPROJECT = os.getcwd()[:os.getcwd().find('/jupyter_notebook')]
sys.path.insert(0, MYPROJECT)
# django 3.0+ 需加入 os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true" 才能使用 orm, (https://docs.djangoproject.com/zh-hans/3.0/topics/async/)
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
import django
django.setup()
image.png

接着就能使用项目里的文件和 ORM 了,如使用 User


image.png

顺便推荐一个超好用的 chrome 插件 眼睛护航


image.png

夜间模式开启后是这样的:


image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。