Mac系统PyCharm尝试cookiecutter-django

过程中需要科学上网,比如mac下的ss(全局模式)+proxifier,否则无法连接或非常慢。

只ss是不够的,只ss没法让命令行科学上网。

安装cookiecutter

anaconda安装cookiecutter

conda install -c conda-forge cookiecutter

参考:https://anaconda.org/conda-forge/cookiecutter

利用cookiecutter-django新建项目

新建项目

先定义项目名称:cookiecutter_django_demo

在项目应该存放的目录中,运行一下代码:

yangMac:development yanghuang$ cookiecutter https://github.com/pydanny/cookiecutter-django
project_name [My Awesome Project]: cookiecutter_django_demo
project_slug [cookiecutter_django_demo]: 
# 省略的部分都是默认值,直接回车确认
timezone [UTC]: Asia/Shanghai
use_pycharm [n]: y
 [SUCCESS]: Project initialized, keep up the good work!
yangMac:development yanghuang$ 

参考:https://github.com/pydanny/cookiecutter-django

新建PostgreSQL数据库

先定义数据库用户是:postgres

先定义数据库密码是:postgresxxx

Mac安装PostgreSQL:

brew install postgresql

initdb /usr/local/var/postgres

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

createdb

yangMac:development yanghuang$ psql
psql (11.1)
Type "help" for help.
yanghuang=# CREATE USER postgres WITH PASSWORD 'postgresxxx';
CREATE ROLE
yanghuang=# DROP DATABASE postgres;
DROP DATABASE
yanghuang=# CREATE DATABASE postgres OWNER postgres;
CREATE DATABASE
yanghuang=# GRANT ALL PRIVILEGES ON DATABASE postgres to postgres;
GRANT
yanghuang=# ALTER ROLE postgres CREATEDB;
ALTER ROLE
yanghuang=# exit
  1. 主要参考:https://www.jianshu.com/p/10ced5145d39

  2. brew若出现以下信息:

    xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

    则需要:

    xcode-select --install

新建venv并尝试运行

通常将所有项目的venv放到一个统一的目录中,如envs;

envs可以跟项目的统一总目录同级别。

先定义对应venv是:cookiecutter_django_venv

在envs目录中,运行以下代码:

python3.6 -m venv cookiecutter_django_venv

source cookiecutter_django_venv/bin/activate

#切换到项目目录
cd ../development/cookiecutter_django_demo/

pip install -r requirements/local.txt

createdb cookiecutter_django_demo -U postgres --password postgresxxx

export DATABASE_URL=postgres://postgres:postgresxxx@127.0.0.1:5432/cookiecutter_django_demo

python manage.py migrate

python manage.py runserver 0.0.0.0:8000

参考:https://cookiecutter-django.readthedocs.io/en/latest/developing-locally.html#setting-up-development-environment

浏览器打开http://0.0.0.0:8000/,效果如下:

pycharm管理新建的cookiecutter-django项目

pycharm打开项目目录,并关联venv

pycharm调试

运行效果如下:

pycharm关联数据库