pycharm创建django项目linux部署

大家好,我是烤鸭:
pytho部署web项目比java简单一点,虽然springboot内置了tomcat。
环境:
pycharm专业版python3.6


1.安装python
python下载:
https://www.python.org/downloads/
我使用的3.6版本

 

 

 

2.配置环境变量

path目录设置到python的安装目录

 

 

 

 


3.创建django项目

 

会在你生成目录的env下自动安装所需的环境和库

如下图:

4.说一下主要文件:

 

settings.py 配置文件

urls.py 路由

templates 静态资源

manage.py 主程序

wsgi.py 网关接口

 

5.创建自己的模块

打开终端:

 

python manage.py startapp myapp

 

6.注册模块

 

7.编写路由

在myapp下的view.py里编写index方法

 

 

import datetimefrom django.http import HttpResponse
from django.shortcuts import render# Create your views here.
def index(request):s = 'Hello World!'current_time = datetime.datetime.now()html = '<html><head></head><body><h1> %s </h1><p> %s </p></body></html>' % (s, current_time)return HttpResponse(html)

 

 

 

 

8.添加映射

在url里配置访问路径,类似java中某个controller的requestMapping

 

9.启动项目

 

上一张访问成功的图:

 

10.返回json数据

按照上面的写法,添加路由和映射

views.py加上方法:

 

import datetime
import jsonfrom django.http import HttpResponse
from django.shortcuts import render# Create your views here.
def index(request):s = 'Hello World!'current_time = datetime.datetime.now()html = '<html><head></head><body><h1> %s </h1><p> %s </p></body></html>' % (s, current_time)return HttpResponse(html)def indexJson(request):current_time = datetime.datetime.now()resp = {'code': '100', 'message': '成功','data': current_time.strftime('%Y-%m-%d %H:%M:%S')}return HttpResponse(json.dumps(resp), content_type="application/json")

 

 

 

 

 

urls.py增加映射:

 

from django.contrib import admin
from django.urls import pathfrom myapp import viewsurlpatterns = [path('admin/', admin.site.urls),path('index/', views.index),path('indexJson/', views.indexJson),
]

 

 

 

 

 

 

 

这样返回的就是标准的json格式的数据了

 

11.关于花生壳配置映射,但是无法访问

settings.py改

 

ALLOWED_HOSTS = ['*'
]

 

 

这样就允许所有的ip访问

 

 

 

12.数据库配置

 

13.创建数据库实体对象

编写models.py

 

from django.db import modelsclass users(models.Model):# 如果没有models.AutoField,默认会创建一个id的自增列a_id = models.IntegerField()name = models.TextField()create_time = models.DateTimeField()

 

 

 

 

 

views.py

 

def list(request):result_set = models.users.objects.all().values('a_id', 'name', 'create_time')data_list = result_set[:]  # queryset转为listprint(type(list(data_list)))data = list(data_list)resp = {'code': '100', 'message': '查询成功' , "data": data}print(json.dumps(resp, cls=CJsonEncoder))return HttpResponse(json.dumps(resp, cls=CJsonEncoder), content_type="application/json")class CJsonEncoder(json.JSONEncoder):def default(self, obj):if isinstance(obj, datetime):return obj.strftime('%Y-%m-%d %H:%M:%S')elif isinstance(obj, datetime.date):return obj.strftime('%Y-%m-%d')else:return json.JSONEncoder.default(self, obj)

 

 

 

 

另外说一下:

关于返回列表数据,时间无法转json的问题,所以增加了

CJsonEncoder方法

 

14.linux部署

之前一直在找打包的方法,类似java打成jar包,可以直接java -jar

python好像不需要打包,直接把pycharm的打包文件夹复制到linux服务器上。

14.1 安装python

wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz
tar Jxvf Python-3.6.4.tar.xz
cd Python-3.6.4
./configure --prefix=/usr/local/python3
make && make install

 

因为我之前安装的版本是python2,现在是python3,但是python -v 还是python2.7

这是推荐一篇:

python2升级到python3
https://jingyan.baidu.com/article/86112f137e502a2736978763.html

上面文章的核心内容:

让系统默认使用Python 3.4.3

这里强调一下,读者在更加本经验操作,不是像白痴一样什么都不懂就操作。。。关于截图中删除/usr/bin/python的操作。请先使用ls -al /usr/bin/python 查看下这个软链接指向的文件。或者先将原来的python软连接重名 mv /usr/bin/python /usr/bin/python2.7.5以便后面好恢复。 上面我们已经将Python 3.4.3安装完成,但是我们进入shell后,查看python版本号: python -V,发现python还是2.7.5版本。升级python之后由于将默认的python指向3.4.3以后,yum不能正常使用,需编辑下yum的配置文件:vi /usr/bin/yum,这里需要先将原来的python软连接重名 mv /usr/bin/python /usr/bin/python2.7.5把文件头部的#!/usr/bin/python改成#!/usr/bin/python2.7.5保存退出即可;我们建立一个新的链接:ln -s /usr/local/bin/python3.4 /usr/bin/python检验python指向是否成功:python -V

14.2 安装项目需要的库

 

升级pip到最新版本:

pip install --upgrade pip

  requests包:

pip install requests

  mysql包和libmysqlclient包:

yum -y install mysql-develyum install libmysqlclient-dev

  centos 7 已经没有 libmysqlclient-dev这个了,可以使用 

pip install mysqlclient

  安装多个pip,指定切换pip的ln软连接

 使用pip安装的时候,不是最新版的pipls -al /usr/bin/pip  查看下这个软链接指向的文件重命名软连接 mv /usr/bin/pip /usr/bin/pip2.7.5ln -s /usr/local/bin/pip3.4 /usr/bin/pip
(/usr/local/bin/pip3.4是指定安装pip的路径)

报错如下:(没报错的跳过)

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfileFile "/usr/libexec/urlgrabber-ext-down", line 28except OSError, e:^
SyntaxError: invalid syntaxFile "/usr/libexec/urlgrabber-ext-down", line 28except OSError, e:^
SyntaxError: invalid syntaxFile "/usr/libexec/urlgrabber-ext-down", line 28except OSError, e:^
SyntaxError: invalid syntax

解决方案:

打开/usr/libexec/urlgrabber-ext-down,修改第一行,同上面百度经验的,

改为#! /usr/bin/python2.7.5
 

报这个错的:

error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory

解决方案:

https://www.cnblogs.com/sixiong/p/5711091.html

安装django:

pip install Django
 This may be because you are using a version of pip that doesn'tunderstand the python_requires classifier. Make sure youhave pip >= 9.0 and setuptools >= 24.2, then try again:$ python -m pip install --upgrade pip setuptools$ python -m pip install djangoThis will install the latest version of Django which works on yourversion of Python. If you can't upgrade your pip (or Python), requestan older version of Django:$ python -m pip install "django<2"

进到项目目录,运行启动命令

$ nohup python manage.py runserver 0.0.0.0:8888 &

 

这时候访问服务器的8888端口就可以访问到了。

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/413015.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【USACO15DEC】最大流Max Flow

题面 FJ给他的牛棚的N(2≤N≤50,000)个隔间之间安装了N-1根管道&#xff0c;隔间编号从1到N。所有隔间都被管道连通了。 FJ有K(1≤K≤100,000)条运输牛奶的路线&#xff0c;第i条路线从隔间si运输到隔间ti。一条运输路线会给它的两个端点处的隔间以及中间途径的所有隔间带来一个…

[css] 用css给一个元素加边框有哪些方法?

[css] 用css给一个元素加边框有哪些方法&#xff1f; :scope {border: 3px solid black;box-shadow: 0 0 0 1px black; /*不影响布局,无限叠加*/outline: 1px solid black; /*不支持圆角*/background-image: url("data:image/svgxml,%3Csvg xmlnshttp://www.w3.org/2000/…

java格式化html代码

/*** 格式化html代码* param model* param html* return*/RequestMapping("/formatHtml.dhtml")ResponseBodypublic M formatHtml(Model model,String html) {if(StringUtils.isNotBlank(html)) {try {Document doc Jsoup.parseBodyFragment(html);html doc.body()…

利用Android Studio快速搭建App

大家好&#xff0c;我是烤鸭: 给大家分享一个简单的用Android Studio快速搭建app 工具&#xff1a;Android Studio 64位 专业版 插件:Datepicker Timepicker okhttp 实现需求&#xff1a;界面上选择时间&#xff0c;发get/post请求到后台&#xff0c;获取选择的时间。1.修改And…

[css] 相邻兄弟选择器、后代选择器和子选择器三者有什么区别?

[css] 相邻兄弟选择器、后代选择器和子选择器三者有什么区别&#xff1f; 后代选择器与子选择的关系&#xff1a;后代选择器>子选择器。 后代选择器&#xff1a;包括父元素的子元素以及孙子元素&#xff08;代表符号&#xff1a;空格&#xff09;子选择器&#xff1a;包括父…

CompletableFuture的多线程和异步监听实现

大家好,我是烤鸭&#xff1a;今天给大家说的是多线程并发的异步监听的情况。这里不得不说一下CompletableFuture这个类&#xff0c;普通我们执行多线程的时候只需要另外启动一条线程。 说一下线程的3种方式&#xff1a;extends Thread&#xff0c;implements Runnable&#xff…

DCF:A Dataflow-Based Collaborative Filtering Trainging Algorithm

Abstratct:描述了当前协同过滤算法两大技术alternating least square(ALS,最小二乘法)和gradient descent(GD)的确定&#xff1a;原文&#xff1a;Existing collaborative filtering techniques are implemented with either alternating least square algorithm or gradient d…

[css] 举例说明你对相邻兄弟选择器的理解

[css] 举例说明你对相邻兄弟选择器的理解 divp{ //相邻兄弟选择器 background: red; } 符合两个条件就会被选中&#xff1a; 1.紧邻在另一个元素后面 2.两者父元素相同个人简介 我是歌谣&#xff0c;欢迎和大家一起交流前后端知识。放弃很容易&#xff0c; 但坚持一定很酷。欢…

HashMap jdk1.7和1.8概述

大家好&#xff0c;我是烤鸭&#xff1a;这是一篇关于HashMap的概述和底层原理的介绍。算是网上很多帖子的综合和我自己的一点想法。HashMap在jdk1.8以前是数组链表。在jdk1.8以后是数组链表红黑树。一点点分析数据结构。1. Map中的entry对象: static class Node<K,V> im…

springboot整合redis修改分区

转载的地址&#xff1a;https://blog.csdn.net/m0_37659871/article/details/81024068#commentBox springboot整合redis修改分区 问题由来 最近使用springboot整合redis&#xff0c;一个系统动态数据源连接不同数据库&#xff0c;缓存使用的redis&#xff0c;那么就需要将不同…

[css] 你是怎么设计css sprites(精灵图)的?有哪些技巧?

[css] 你是怎么设计css sprites&#xff08;精灵图&#xff09;的&#xff1f;有哪些技巧&#xff1f; 首先肯定不会去用PS量&#xff0c;那太费时间了~ 没有webpack以前&#xff0c;用Gulp的gulp.spritesmith插件&#xff0c;这里附上配置源码/* gulpfile.js */ const gulp …

iOS沙盒文件夹及获取路劲方法

iPhone沙盒中有四个文件夹&#xff0c;分别是&#xff1a;documents、tmp、app、library. 1、Documents &#xff1a;用户生成的文档或数据&#xff0c;或者应用不能重新新创建的数据&#xff0c;存储在/Documents目录下&#xff0c;并且会被自动备份到iCloud&#xff1b; 2、A…

springboot多环境加载yml和logback配置

大家好&#xff0c;我是烤鸭&#xff1a;这是一篇关于springboot多环境加载yml和logback配置文件。环境&#xff1a;开发工具 idea(推荐)/eclipse(对yml支持不好)jdk 1.8springboot 1.5.6.RELEASE 1. yml和logback文件1.1 结构,如图所示&#xff1a;1.2 application.yml (默…

[css] 请描述下你对translate()方法的理解

[css] 请描述下你对translate()方法的理解 Single length/percentage value一个长度值或百分比表示X轴和Y轴使用一样的值进行二维上的平移。等同于translate() &#xff08;2D 平移&#xff09;函数指定单个值。Two length/percentage values两个长度值或百分比表示在二维上分…

laydate闪退

1,解决方案一&#xff1a;加 trigger: ‘click’ laydate.render({ elem: this ,format:‘yyyy-MM-dd HH:mm:ss’ ,type:‘datetime’ ,trigger: click’ }); 解决方案二&#xff1a; $("#"dateControlId).removeAttr(“lay-key”);

汇编实验二

》实验结论 1.使用Debug将下面的程序写入内存&#xff0c;逐条执行&#xff08;见1-1&#xff09;&#xff0c;根据指令执行后的实际情况填空&#xff08;见1-2&#xff09; p.s. 已经按实验要求将使用 e 命令将内存单元 0021:0 ~0021:7 连续 8 个字节数据修改为 30H, 31H, 32H…

springboot中的拦截器interceptor和过滤器filter,多次获取request参数

大家好&#xff0c;我是烤鸭&#xff1a; 这是一篇关于springboot的拦截器(interceptor)和过滤器(Filter)。 先说一下过滤器和拦截器。区别&#xff1a;1. servlet请求&#xff0c;顺序&#xff1a;Filter ——> interceptor。2. Filter的作用是对所有进行过滤&#xff…

[css] 怎样去除图片自带的边距?

[css] 怎样去除图片自带的边距&#xff1f; 空隙产生的原因&#xff0c;换行符&#xff0c;空格符&#xff0c;制表符等你空白符&#xff0c;字体不为0的情况下&#xff0c;都会产生一个字符的空隙&#xff0c;空格符好会占据一定宽度&#xff0c;使用inline-block会产生元素间…

Java删除list

方案1>&#xff1a;for循环删除&#xff1a;注意从大到小遍历&#xff0c;不是从小到大&#xff1b; /*** 删除选中项*/private void deleteCheckedItem() {// list&#xff1a;初始化所有的数据&#xff1b;count&#xff1a;最后角标int count list.size() - 1 ;//从大到…

Unable to locate the default servlet for serving static content. Please set the 'defaultServletName'

大家好&#xff0c;我是烤鸭。 今天分享一个莫名其妙的异常及解决方式。 环境&#xff1a; tomcat6 jdk 1.6 异常主体&#xff1a; java.lang.IllegalStateException: Unable to locate the default servlet for serving static content. Please set the defaultServletName p…