Fork me on GitHub

Django项目远程开发环境搭建

一、虚拟环境搭建

1、工具或者软件

  • centos阿里云服务器
  • Anaconda3-5.2.0-Linux-x86_64.sh

注意的是官网下载Anaconda是比较慢的,可以去https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/上下载对应的Anaconda:

[root@iZwz9ba9y5k8ij7xf2boohZ software]# wget https://mirrors.tuna.tsinghua.edu.cn   \
                        /anaconda/archive/Anaconda3-5.2.0-Linux-x86_64.sh

2、虚拟环境搭建

  • 安装
[root@iZwz9ba9y5k8ij7xf2boohZ software]# bash Anaconda3-5.2.0-Linux-x86_64.sh

安装过程只需要回车以及yes确定即可,当然默认安装在root目录下,所以你可以进行更改:

"""
...
Anaconda3 will now be installed into this location:
/root/anaconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/root/anaconda3] >>> /software
PREFIX=/software
...

"""

在装完后环境变量写入到/root/.bash_profile文件中只需要source这个文件立即生效即可,这样就可以使用conda等命令了。

  • 配置

anaconda的源配置很重要,配置的好下载很快:

# 添加源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --set show_channel_urls yes

# 换回默认源
conda config --remove-key channels

上面的操作实际上就是在/root/.condarc文件中添加如下信息:

show_channel_urls: true
channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - defaults

3、虚拟环境创建、使用

# 1、虚拟环境创建
[root@iZwz9ba9y5k8ij7xf2boohZ ~]# conda create -n qaWebEnv python=3.8

# 2、查看虚拟环境
[root@iZwz9ba9y5k8ij7xf2boohZ ~]# conda info --env
# conda environments:
#
base                  *  /project/software/anaconda3
qaWebEnv                 /project/software/anaconda3/envs/qaWebEnv

# 3、激活虚拟环境,使用source激活
(base) [root@iZwz9ba9y5k8ij7xf2boohZ ~]# source activate qaWebEnv
(qaWebEnv) [root@iZwz9ba9y5k8ij7xf2boohZ ~]#

二、创建项目

1、安装cookiecutter

(qaWebEnv) [root@iZwz9ba9y5k8ij7xf2boohZ ~]#  pip install "cookiecutter>=1.7.0"

2、下载模板

(qaWebEnv) [root@iZwz9ba9y5k8ij7xf2boohZ ~]# cookiecutter https://github.com/pydanny/cookiecutter-django.git

但是这里容易出的问题就是卡住不动了,下载不下来,所以解决的方法就是去手动下载源码然后将其放到/root/.cookiecutters目录下:

[root@iZwz9ba9y5k8ij7xf2boohZ .cookiecutters]# ls
cookiecutter-django

3、创建项目

(qaWebEnv) [root@iZwz9ba9y5k8ij7xf2boohZ project]# cookiecutter https://github.com/pydanny/cookiecutter-django.git
You've downloaded /root/.cookiecutters/cookiecutter-django before. Is it okay to delete and re-download it? [yes]: n
Do you want to re-use the existing version? [yes]: y
project_name [My Awesome Project]: qaWeb
project_slug [qaweb]: 
description [Behold My Awesome Project!]: a website
author_name [Daniel Roy Greenfeld]: Bright
domain_name [example.com]: 
email [bright@example.com]: 
version [0.1.0]: 
Select open_source_license:
1 - MIT
2 - BSD
3 - GPLv3
4 - Apache Software License 2.0
5 - Not open source
Choose from 1, 2, 3, 4, 5 [1]: 5
timezone [UTC]: Asia/Shanghai
windows [n]: n
use_pycharm [n]: y
use_docker [n]: 
Select postgresql_version:
1 - 12.3
2 - 11.8
3 - 10.8
4 - 9.6
5 - 9.5
Choose from 1, 2, 3, 4, 5 [1]: 
Select js_task_runner:
1 - None
2 - Gulp
Choose from 1, 2 [1]: 
Select cloud_provider:
1 - AWS
2 - GCP
3 - None
Choose from 1, 2, 3 [1]: 
Select mail_service:
1 - Mailgun
2 - Amazon SES
3 - Mailjet
4 - Mandrill
5 - Postmark
6 - Sendgrid
7 - SendinBlue
8 - SparkPost
9 - Other SMTP
Choose from 1, 2, 3, 4, 5, 6, 7, 8, 9 [1]: 
use_async [n]: 
use_drf [n]: 
custom_bootstrap_compilation [n]: 
use_compressor [n]: y
use_celery [n]: y
use_mailhog [n]: 
use_sentry [n]: 
use_whitenoise [n]: 
use_heroku [n]: 
Select ci_tool:
1 - None
2 - Travis
3 - Gitlab
Choose from 1, 2, 3 [1]: 
keep_local_envs_in_vcs [y]: n
debug [n]: y
 [SUCCESS]: Project initialized, keep up the good work!

此时就生成了对应的文件和配置:

(qaWebEnv) [root@iZwz9ba9y5k8ij7xf2boohZ project]# tree qaweb/
qaweb/
├── config
│   ├── celery_app.py
│   ├── __init__.py
│   ├── settings
│   │   ├── base.py
│   │   ├── __init__.py
│   │   ├── local.py
│   │   ├── production.py
│   │   └── test.py
│   ├── urls.py
│   └── wsgi.py
├── docs
│   ├── conf.py
│   ├── __init__.py
│   ├── make.bat
│   ├── Makefile
│   └── _source
│       ├── howto.rst
│       ├── index.rst
│       ├── pycharm
│       │   ├── configuration.rst
│       │   └── images
│       │       ├── 1.png
│       │       ├── 2.png
│       │       ├── 3.png
│       │       ├── 4.png
│       │       ├── 7.png
│       │       ├── 8.png
│       │       ├── f1.png
│       │       ├── f2.png
│       │       ├── f3.png
│       │       ├── f4.png
│       │       ├── issue1.png
│       │       └── issue2.png
│       └── users.rst
├── locale
│   └── README.rst
├── manage.py
├── pytest.ini
├── qaweb
│   ├── conftest.py
│   ├── contrib
│   │   ├── __init__.py
│   │   └── sites
│   │       ├── __init__.py
│   │       └── migrations
│   │           ├── 0001_initial.py
│   │           ├── 0002_alter_domain_unique.py
│   │           ├── 0003_set_site_domain_and_name.py
│   │           └── __init__.py
│   ├── __init__.py
│   ├── static
│   │   ├── css
│   │   │   └── project.css
│   │   ├── fonts
│   │   ├── images
│   │   │   └── favicons
│   │   │       └── favicon.ico
│   │   ├── js
│   │   │   └── project.js
│   │   └── sass
│   │       ├── custom_bootstrap_vars.scss
│   │       └── project.scss
│   ├── templates
│   │   ├── 403.html
│   │   ├── 404.html
│   │   ├── 500.html
│   │   ├── account
│   │   │   ├── account_inactive.html
│   │   │   ├── base.html
│   │   │   ├── email_confirm.html
│   │   │   ├── email.html
│   │   │   ├── login.html
│   │   │   ├── logout.html
│   │   │   ├── password_change.html
│   │   │   ├── password_reset_done.html
│   │   │   ├── password_reset_from_key_done.html
│   │   │   ├── password_reset_from_key.html
│   │   │   ├── password_reset.html
│   │   │   ├── password_set.html
│   │   │   ├── signup_closed.html
│   │   │   ├── signup.html
│   │   │   ├── verification_sent.html
│   │   │   └── verified_email_required.html
│   │   ├── base.html
│   │   ├── pages
│   │   │   ├── about.html
│   │   │   └── home.html
│   │   └── users
│   │       ├── user_detail.html
│   │       └── user_form.html
│   ├── users
│   │   ├── adapters.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── forms.py
│   │   ├── __init__.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   └── __init__.py
│   │   ├── models.py
│   │   ├── tasks.py
│   │   ├── tests
│   │   │   ├── factories.py
│   │   │   ├── __init__.py
│   │   │   ├── test_forms.py
│   │   │   ├── test_models.py
│   │   │   ├── test_tasks.py
│   │   │   ├── test_urls.py
│   │   │   └── test_views.py
│   │   ├── urls.py
│   │   └── views.py
│   └── utils
│       ├── context_processors.py
│       ├── __init__.py
│       └── storages.py
├── README.rst
├── requirements
│   ├── base.txt
│   ├── local.txt
│   └── production.txt
├── setup.cfg
└── utility
    ├── install_os_dependencies.sh
    ├── install_python_dependencies.sh
    ├── requirements-bionic.apt
    ├── requirements-buster.apt
    ├── requirements-jessie.apt
    ├── requirements-stretch.apt
    ├── requirements-trusty.apt
    └── requirements-xenial.apt

28 directories, 103 files

三、本地搭建 

上面的虚拟环境以及项目都是在云环境创建完成,现在通过PyCharm来进行连接同步。

1、本地新建项目目录

本地新建一个空文件夹,名称和远程主机项目名称一致,然后使用PyCharm打开:

2、配置远程连接

 先打开远程配置的相关页面:Tools->Deployment->Configuration

在配置文件中首先创建一个server,然后再Connection页中进行连接的配置:

 然后Mappings中进行本地和远程目录的映射:

Local path就是本地的目录,Deployment path就是远程中相对于Connection中Root path中后面的路径,也就是Root path+Deployment path=Local path。

另外一个就是Exclude Path这是设置本地或者远程目录中不进行同步的文件的路径。此时不需要进行配置。

3、Python解释器配置

本地开发该项目使用的是远程虚拟环境Python解释器:

点击下面的next按钮,注意的是选择Move this Server to IDE settings(之前选择过,选择没出先该选项了)。

 这样就完成了配置,注意勾选自动上传文件到server项目目录。

4、远程同步

 

   当连接与目录映射完成后将远程主机上的项目文件同步到本地,可以看到Upload to qaWeb、Download from qaWeb、Sync with Deployed to qaWeb,这里选择第三个就是将远程主机与本地可以进行比对,然后同步:

5、Django Server配置 

  • Django Server Configurations

  •  Languages & FrameWorks

 在PyCharm上设置需要注意这三点:

  • 设置本地与远程的映射
  • 设置Python解释器
  • 启动django支持(设置Settings就是为了PyCharm中启动Django项目)
posted @ 2020-09-20 14:46  iveBoy  阅读(726)  评论(0编辑  收藏  举报
TOP