How do I set up Jupyter/IPython Notebook for Django?

后端 未结 8 1295
鱼传尺愫
鱼传尺愫 2020-11-30 17:17

I have been using the method described in this post for setting up IPython Notebook to play nicely with Django. The gist of the method is to create an IPython extension whic

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 18:00

    The following does work for me using win10, Python 3.5, Django 1.10:

    • Install Python with the Anaconda distribution so Jupyter will be installed as well
    • Install Django and install django-extensions:

      pip install Django
      
      pip install django-extensions
      
    • Start a new Django project. You have to do that in that part of your tree of directories which can be accessed by Jupyter later.

      django-admin startproject _myDjangoProject_
      
    • Start Jypter

    • navigate Jupyter to the directory myDjangoProject and enter the first/top myDjangoProject-directory
    • Start within the first/top myDjangoProject-directory a new Jupyter noteboke: new --> Django Shell-Plus
    • enter and run the following piece of code :

      import os
      os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myDjangoProject.settings")
      import django
      django.setup()
      
    • Note that this piece of code is the same as in manage.py, and note that "myDjangoProject.settings" points to myDjangoProject/settings.py

    • Now you can start with examples, e.g.:

      from django.template import Template, Context
      
      template = Template('The name of this project is {{ projectName }}')
      context = Context({'projectName': 'MyJypyterDjangoSite'})
      template.render(context)
      

提交回复
热议问题