网站建设资讯

NEWS

网站建设资讯

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

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)

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


网站题目:DJango-如何快速准备Python虚拟开发环境并快速定制一个项目-创新互联
文章转载:http://cdweb.net/article/ceihgj.html