温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
  • 首页 > 
  • 教程 > 
  • 开发技术 > 
  • DJango-如何快速准备Python虚拟开发环境并快速定制一个项目

DJango-如何快速准备Python虚拟开发环境并快速定制一个项目

发布时间:2020-06-23 22:31:42 来源:网络 阅读:1389 作者:scobe 栏目:开发技术

Django是1.11,Python3.6,Mac OS

中英文混杂,主要为为自己记录用,时间有限,见谅。


  • Create Dev Directory for general project storage

    • $cd ~/projects

    • $mkdir Dev && cd Dev

  • Create Virtual Environment

    • $mkdir project_name && cd project_name

    • $pip install virtualenv virtualenvwrapper

    • $mkdir ~/.venvs

    • $export WORKON_HOME=~/.venvs

    • $export PATH=/Library/Frameworks/Python.framework/Versions/3.6/bin:$PATH

    • $source /Library/Frameworks/Python.framework/Versions/3.6/bin/virtualenvwrapper.sh

    • $mkvirtualenv —python=/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 project_name

    • To activate project_name virtual env:

    • $workon project_name

    • To deactivate project_name, just 

    • $deactivate



  • Install Django & Start Project

    • $pip install django==1.11.4

    • $mkdir src && cd src

    • $django-admin startproject project_name . 

    • (Don’t forget the “.” )

  • Create New Settings Module

    • Currently working in ./src

    • $cd project_name

    • $mkdir settings && cd settings

    • Change BASE_DIR in settings.py:

    • BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

    • To

    • BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

    • Move default settings.py into new settings module and rename settings.py to base.py

    • $mv settings.py ./settings/base.py

    • $cd settings/

    • Copy base.py to make new (local.py, dev.py & pro.py) files:

    • $cp base.py local.py 

    • $cp base.py dev.py

    • $cp base.py pro.py

    • To use local.py as our starting point since we use sqlite3 database. We will move to dev.py with development is going forward and finally pro.py when we decide to release our product.

    • In local.py use the following settings. 


    • from .base import *
      
      DEBUG = True
      
      ALLOWED_HOSTS = []
      
      DATABASES = {
          'default': {
              'ENGINE': 'django.db.backends.sqlite3',
              'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
          }
      }
    • Remove the related contents from base.py accordingly.

    • Edit manage.py and replace os.environ.setdefault("DJANGO_SETTINGS_MODULE", “project_name.settings") with os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project_name.settings.local")



    • Or you can set env variable:

    • $export DJANGO_SETTINGS_MODULE = project_name.settings.local

    • Or you can directly run manage.py with settings options.e.g:

    • $python manage.py runserver —settings=project_name.settings.local

    • $python manager.py createsuperuser (to create a super user account)

    • Openhttp://127.0.0.1:8000/, you should see the successful message.

    • You also can loginhttp://127.0.0.1:8000/admin using the super user. There you can create and update groups and users. 

    • You can use pip freeze to save a list for installed packages.

    • $pip freeze > requirements.txt

    • (You can use $pip install-r requirements.txt to install all the packages for dependency)



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI