PyCharm‘s Project Deployment

当在本地写完项目,部署到服务器上调试的时候,难免会碰到代码的修修改改,但由于项目在服务器上,修改起来相对麻烦。各路大神或许有自己的方法去解决。这篇博客演示利用PyCharm的Deployment功能, 进行项目的本地编写,远程服务器同步修改代码的功能。

环境:

  本地:PyCharm2018.1 Professional + Python3.5.4 + win10

  远程:VMware下的Centos7.3 + python3.5.4


No.1. 将本地代码上传到服务器

配置项目,跑起来就行了

from django.shortcuts import render, redirect,HttpResponse
from app01 import models
# Create your views here.




def login(request):
    if request.method == 'GET':
        return render(request,'login.html')
    elif request.method == 'POST':

        user = request.POST.get('user')
        pwd = request.POST.get('pwd')
        # 下面两行代码把数据保存到数据库,然后有了用户名和密码,就可以注释掉了
        # models.User.objects.create(user=user, pwd=pwd)
        # models.User.objects.all()
        # 查询
        user_obj = models.User.objects.filter(user=user, pwd=pwd)
        if user_obj:
            u = user_obj.first()
            print(u.user)
            print(u.pwd)
            request.session['a'] = user + pwd
            return redirect('/index')
        return HttpResponse('没有用户!请在views文件的第16、17行注释打开,注册用户,再注释掉')
def index(request):
    # a = request.session['a']
    # print(a)
    if request.is_ajax():
        print(request.POST.get('username'))
        return HttpResponse('AAAA')
    return render(request, 'index.html')
views.py
from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^login/', views.login),
    url(r'^index/', views.index),
]
urls.py
from django.db import models

# Create your models here.
class User(models.Model):
    user = models.CharField(max_length=32)
    pwd = models.CharField(max_length=32)
model.py
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>login</title>
</head>
<body>
<div>
    <form action="http://127.0.0.1:8000/login/" method="post">
        {% csrf_token %}
        <input type="text" name="user" id="" placeholder="username">
        <input type="password" name="pwd" id="" placeholder="password">
        <input type="submit" name="" id="" value="提交">

    </form>
    
</div>
</body>
</html>
login.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="x-ua-compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>index</title>

</head>
<body>
<div>
<h1>successful</h1>
    {% csrf_token %}
    <input type="button" name="" id="sure" value="提交">
</div>
{#<script src="{% static 'ckeditor/ckeditor.js' %}"></script>#}
<script src="https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script>
    var csrf = $("[name='csrfmiddlewaretoken']").val();
    alert($('h1').text(),);
    $('#sure').click(function(){
        $.ajax({
            url:'/index/',
            type:'POST',
            data:{'username':$('h1').text(), "csrfmiddlewaretoken":csrf},
            success:function (data) {
                alert(data);
            }
        })
    });

</script>
</body>
</html>
index.html
# settings有三点需要注意,templates和把app添加到INSTALLED_APPS里,然后是static。
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app01',
]


TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]


STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
settings

 

No.2 往远程推代码

2.1 配置打开PyCharm的菜单栏的Tools --> Deployment --> Configure

2.2 此时点进去,会提示【Please add a web server to configure】
  1. 点击+号,add server
  2. 填写server的name
  3. 选择传输类型
  4. server配置完毕,点击OK 

 

 2.3 配置test1的【Connection】
  1. 此时name和type可以更改
  2. 添加【SFTP host】,port端口一般不用动,host则是你的服务器ip
  3. 添加服务器的username和password  # 需要注意的是,user最好为root用户,因为会涉及到创建文件夹等操作
  4. 点击【Test SFTP connection】,成功则会出现successful的成功提示,错误的话,你就要检查各选项是否添加不正确,服务器是否支持访问
需要注意的是,root path为项目将来要放在服务器的根目录,后面的项目就在此目录下

2.4 配置test1的【Mappings】
  1. local path,本地的项目路径
  2. server端的path,点击【···】选择创建好的目录, 这个path基于Connection下的root path
  3. 选择好目录,点击OK
  4. 都设置完毕,点击OK

2.5 配置test1的【Excluded Paths】
1. add local path  # 添加本地无需上传的文件或目录路径
2. 添加结果如2所示
3. add deployment path # 拉代码时,远程项目不需要下载的文件或目录路径
4. remove path  # 删除路径

2.6 配置【Options,【Tools】-- 【Deployment】--【Options】
1. create empty directories    # 创建空目录,如果远程没有这个目录则创建
2. 同步方式,是选择Ctrl+s还是always或是never

2.7 将项目上传到服务器
PyCharm --> Tools --> Deployment --> Upload to test1(取决于你的项目名)  

注意大坑来了!在点击上传时注意此时你的左侧选择的文件。如当我在选择4时,点击1 upload to test1,那么只是把单个文件上传到远程的服务器,如果我选择3,则只是把这个目录及这里面的文件上传,所以,如果要上传整个项目,那么将鼠标点击到2,项目根目录,然后在点击1上传,这样,排除那些你设定好的无需上传的文件或目录,整个项目都会上传到远程服务器。

No.3 设置代码自动上传

3.1 代码自动上传方式1

Ctrl+s. PyCharm --> Tools --> Deployment --> Optinos,当编辑完毕,Ctrl+S保存就会自动提交到服务器,我们在前面有提到过这一步

3.2 代码自动上传方式2

无需手动的Ctrl+S. PyCharm --> Tools --> Deployment --> Automatic Upload(always)   当选择这种方式时,你在PyCharm中写完代码之后,一个很小的等待之后,新更新的代码就会自动的同步到服务器,无需手动保存了

No.3 本地通过PyCharm查看服务器目录

PyCharm --> Tools --> Deployment --> Browse Remote Host   这样,你PyCharm的右边栏就会出现远程服务器的目录

No.4 设置远程解释器

虽然此时我们把代码推到了远程服务器,并且能够调试,但是,此时用的解释器还是我们本地的解释器,那么,我们如何设置使用远程的解释器呢?

 

 

 


欢迎指正

参考摘自:

  https://blog.csdn.net/zhaihaifei/article/details/53691873

  

 

转载于:https://www.cnblogs.com/Neeo/p/9246741.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值