DJango-如何快速准备Python虚拟开发环境并快速定制一个项目
2017-08-27 02:58
981 查看
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 like-minded
To activate like-minded virtual env:
$workon like-minded
To deactivate like-minded, just
$deactivate
Install Django & Start Project
$pip install django==1.11.4
$mkdir src && cd src
$django-admin startproject likeminded .
(“like-minded” is not valid identifier. Don’t forget the “.” )
Create New Settings Module
Currently working in ./src
$cd likeminded
$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.
Remove the related contents from base.py accordingly.
Edit manage.py and replace os.environ.setdefault("DJANGO_SETTINGS_MODULE", “likeminded.settings") with os.environ.setdefault("DJANGO_SETTINGS_MODULE", "likeminded.settings.local")
Or you can set env variable:
$export DJANGO_SETTINGS_MODULE = likeminded.settings.local
Or you can directly run manage.py with settings options.e.g:
$python manage.py runserver —settings=likeminded.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)
中英文混杂,主要为为自己记录用,时间有限,见谅。
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 like-minded
To activate like-minded virtual env:
$workon like-minded
To deactivate like-minded, just
$deactivate
Install Django & Start Project
$pip install django==1.11.4
$mkdir src && cd src
$django-admin startproject likeminded .
(“like-minded” is not valid identifier. Don’t forget the “.” )
Create New Settings Module
Currently working in ./src
$cd likeminded
$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", “likeminded.settings") with os.environ.setdefault("DJANGO_SETTINGS_MODULE", "likeminded.settings.local")
Or you can set env variable:
$export DJANGO_SETTINGS_MODULE = likeminded.settings.local
Or you can directly run manage.py with settings options.e.g:
$python manage.py runserver —settings=likeminded.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)
相关文章推荐
- ubuntu 下搭建一个python3的虚拟环境(用于django配合postgresql数据库开发)
- Python web框架Django学习(1)——在win7 64bit下配置开发环境Django:一个可以使Web开发工作愉快并且高效的Web开发框架。 使用Django,使你能够以最小的代价构建和
- 腾讯云快速完成python3.6开发环境搭建与django应用部署
- 在Eclipse中如何利用在Anaconda中建立的Python虚拟环境进行开发
- django开发环境搭建和创建一个简单的django项目
- pythonWeb -- Django开发-创建一个Django项目
- python3开发进阶-Django框架学习前的小项目(一个简单的学员管理系统)
- Python之Django商城项目实战(一):搭建开发环境
- 如何正确开始一个python项目(django为例)
- windows下如何快速构建一个python3项目并发布
- windows 下的Python虚拟环境(vitrualen)pycharm创建Django项目
- 如何利用pyenv 和virtualenv 在单机上搭建多版本python 虚拟开发环境
- python使用Visual Studio 2015作为IDE开发django项目的环境部署
- 如何理解与快速构建python编程程环境,eclipse+pydev插件+python虚拟平台
- 使用Python开发环境Wing IDE如何设立项目详解
- Python之Django商城项目实战(一):搭建开发环境
- vue开发环境准备-创建一个vue项目
- 如何在虚拟环境中创建django项目