日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

pycharm创建django项目linux部署

發(fā)布時間:2023/12/10 linux 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pycharm创建django项目linux部署 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

大家好,我是烤鴨:
pytho部署web項目比java簡單一點,雖然springboot內(nèi)置了tomcat。
環(huán)境:
pycharm專業(yè)版python3.6


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

?

?

?

2.配置環(huán)境變量

path目錄設置到python的安裝目錄

?

?

?

?


3.創(chuàng)建django項目

?

會在你生成目錄的env下自動安裝所需的環(huán)境和庫

如下圖:

4.說一下主要文件:

?

settings.py?配置文件

urls.py?路由

templates?靜態(tài)資源

manage.py?主程序

wsgi.py?網(wǎng)關接口

?

5.創(chuàng)建自己的模塊

打開終端:

?

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數(shù)據(jù)

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

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格式的數(shù)據(jù)了

?

11.關于花生殼配置映射,但是無法訪問

settings.py改

?

ALLOWED_HOSTS = ['*' ]

?

?

這樣就允許所有的ip訪問

?

?

?

12.數(shù)據(jù)庫配置

?

13.創(chuàng)建數(shù)據(jù)庫實體對象

編寫models.py

?

from django.db import modelsclass users(models.Model):# 如果沒有models.AutoField,默認會創(chuàng)建一個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轉(zhuǎn)為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)

?

?

?

?

另外說一下:

關于返回列表數(shù)據(jù),時間無法轉(zhuǎn)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,現(xiàn)在是python3,但是python -v 還是python2.7

這是推薦一篇:

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

上面文章的核心內(nèi)容:

讓系統(tǒng)默認使用Python 3.4.3

這里強調(diào)一下,讀者在更加本經(jīng)驗操作,不是像白癡一樣什么都不懂就操作。。。關于截圖中刪除/usr/bin/python的操作。請先使用ls -al /usr/bin/python 查看下這個軟鏈接指向的文件。或者先將原來的python軟連接重名 mv /usr/bin/python /usr/bin/python2.7.5以便后面好恢復。?上面我們已經(jīng)將Python 3.4.3安裝完成,但是我們進入shell后,查看python版本號: python -V,發(fā)現(xiàn)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 已經(jīng)沒有 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,修改第一行,同上面百度經(jīng)驗的,

改為#! /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端口就可以訪問到了。

?

總結(jié)

以上是生活随笔為你收集整理的pycharm创建django项目linux部署的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。