Mac系统PyCharm尝试cookiecutter-django

Mac系统PyCharm尝试cookiecutter-django

文章目录

  • [Mac系统PyCharm尝试cookiecutter-django](http://yanghuangblog.com/index.php/archives/10/)
    • 安装cookiecutter
      • anaconda安装cookiecutter
    • 利用cookiecutter-django新建项目
      • 新建项目
      • 新建PostgreSQL数据库
      • 新建venv并尝试运行
    • pycharm管理新建的cookiecutter-django项目
      • pycharm打开项目目录,并关联venv
      • pycharm调试
      • pycharm关联数据库

过程中需要科学上网,比如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:[email protected]: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/,效果如下:

Mac系统PyCharm尝试cookiecutter-django_第1张图片

pycharm管理新建的cookiecutter-django项目

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

Mac系统PyCharm尝试cookiecutter-django_第2张图片

Mac系统PyCharm尝试cookiecutter-django_第3张图片

pycharm调试

Mac系统PyCharm尝试cookiecutter-django_第4张图片

运行效果如下:

Mac系统PyCharm尝试cookiecutter-django_第5张图片

pycharm关联数据库

Mac系统PyCharm尝试cookiecutter-django_第6张图片

Mac系统PyCharm尝试cookiecutter-django_第7张图片

Mac系统PyCharm尝试cookiecutter-django_第8张图片

Mac系统PyCharm尝试cookiecutter-django_第9张图片

你可能感兴趣的:(Mac系统PyCharm尝试cookiecutter-django)