Django的项目的重新部署

来源:互联网 发布:淘宝网店开店流程 编辑:程序博客网 时间:2024/06/06 04:03

作为完全没有接触过python的小白,希望大家多多指教
临时接到部署一个几年前写的Django的项目的重新部署。花了近两天遇到了一些问题写下来,希望能帮助别人也能给自己留个备份
这是mentor发给我部署的指导
a) Install all the python requirements that exist in the requirements.txt file
This will be failing for multiple packages as it is outdated, so wherever it fails, you can manually install the package, and remove the entry from the .txt file

b) Install https://www.phusionpassenger.com/library/install/apache/install/oss/trusty/ - wsgi (and set it up) - I don’t have any experience with this, so you will have to do some research

c) Setup django - follow this tutorial https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

a)$ pip install -r requirements.txt 因为代码年份比较久远,所以很多的package比较久远。每次遇到一个错误,就根据提示的line把package注释了然后手动安装新的package。这样也为后来埋下了一些坑,有一些版本兼容问题。

b) 推荐链接
https://www.phusionpassenger.com/library/install/apache/install/oss/trusty/
Window 不支持。能根据OS 的版本来选择安装方法。非常感谢作者!

c)教程还是清楚明了了。根据我的项目情况我按照教程来创建了 a Python virtual environment。需要注意的是,要把manag.py os.environ.setdefault(“DJANGO_SETTINGS_MODULE”, “xxx.settings”)
xxx.setting 指向自己项目的setting。
按照教程跑项目。

总结一下遇到的问题和解决方法(都不是原创,只是总结一下):
P1
from django.template.engine import Engine
ImportError: No module named engine

由于版本问题,根据我的equirements.txt 安装的djanggo的版本是1.7.2
解决:升级版本 pip install –upgrade django

P2
from django.conf.urls import patterns, include, url
ImportError: cannot import name patterns

由于版本问题,由于我升级了版本,djanggo 1.8+以后的版本不支持以下的写法
这里写图片描述
需要改成:
这里写图片描述
(手动把项目组urls.py的文件都改成这样)

P3
Exception Type: TemplateDoesNotExist at /
Exception Value: base.html
解决
添加BASE_DIR = os.path.dirname(os.path.abspath(file))
去掉 TEMPLATE_DIRS and TEMPLATE_LOADERS.
加上
TEMPLATES = [
{
‘BACKEND’: ‘django.template.backends.django.DjangoTemplates’,
‘DIRS’: [‘/xxx/xxxx’],#你的路径
‘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’,
],
},
},
]

P4
NoReverseMatch at /
u’djdt’ is not a registered namespace
解决
在每个urls.py 的文件下面加上
if settings.DEBUG:
import debug_toolbar
urlpatterns = [
url(r’^debug/’, include(debug_toolbar.urls)),
] + urlpatterns
如下
这里写图片描述

还遇到了很多问题,但是还是比较容易google到的,就不一一列出来啦!

这里写图片描述