欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

IIS 部署 Python Django网站流程(受够了野路子)

程序员文章站 2022-09-04 14:52:31
知道的,百度上搜出来的东西质量令人唏嘘。当你求助的时候多半还得靠自己,或者靠Google 介入正题,详细来一遍流程吧 当然,我是用Visual Studio 2019 来编辑开发Django项目的,如果你也是那么巧了。这可以帮你 如果你Django项目可以在vs上运行,那么关键的是能不能在项目根目录 ......

知道的,百度上搜出来的东西质量令人唏嘘。当你求助的时候多半还得靠自己,或者靠google

介入正题,详细来一遍流程吧

当然,我是用visual studio 2019 来编辑开发django项目的,如果你也是那么巧了。这可以帮你

如果你django项目可以在vs上运行,那么关键的是能不能在项目根目录跑起来,这个需要测试一下。

先安装wfastcgi模块 

pip install wfastcgi

安装成功后,在你python 的根目录 【python\lib\site-packages】下面找到 wfastcgi.py  文件,复制一份放在项目的根目录下面即可。

在你项目根目录下面 按住shift 鼠标右键 菜单,点击 【 在此处打开powershell窗口】输入以下命令

python manage.py runserver

出现

ps e:\git\gitrepository\codeshitpro\djangowebproject> python manage.py runserver
performing system checks...

system check identified no issues (0 silenced).

you have 3 unapplied migration(s). your project may not work properly until you apply the migrations for app(s): admin, auth.
run 'python manage.py migrate' to apply them.
december 29, 2018 - 13:37:18
django version 2.1.4, using settings 'djangowebproject.settings'
starting development server at http://127.0.0.1:8000/
quit the server with ctrl-break.

如果出现跟这个一样那就恭喜你可以继续往后配置了。如果不行,那么pip 安装对应的模块即可

现在打开iis 创建网站,根目录可以指向你项目根目录,没有关系。测试嘛,跑起来就好,规范的事情在规范的时候去做!

接下来给你的django项目添加一个web.config 文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration> 
  <system.webserver>
    <handlers>
      <add name="python fastcgi" path="*" verb="*" modules="fastcgimodule" scriptprocessor="f:\program files (x86)\python\python.exe|e:\git\gitrepository\codeshitpro\djangowebproject\wfastcgi.py" resourcetype="unspecified" />
    
    </handlers>
  </system.webserver>
</configuration>

注:

scriptprocessor 里面以 "|" 分割开的俩段内容,第一个是你python的根目录找到【python.exe】 第二段是复制了【wfastcgi.py】 的项目根目录指向地址,不要写错了。 
接下来哦。要在iis 根节点选中

IIS 部署 Python Django网站流程(受够了野路子)

进入【fastcgi】 中,【添加应用程序】

IIS 部署 Python Django网站流程(受够了野路子)

1 处填写 web.config  中scriptprocessor 第一段,2处填写 第二段

3处点击进入添加三个成员,内容是:

name   value
wsgi_handler
django.core.wsgi.get_wsgi_application()
pythonpath
你的站点根目录
django_settings_module
自定义名称 .settings

 

 

 


 

 

填写完成后确定,重启一下站点,然后运行发现没有样式加载成功,f12 后发现一片报红

接下来操作比较关键了

1.在项目的settings.py 中加入代码

#*********************************发布iis 必须添加的代码***************************************************
site_root = os.path.abspath(os.path.dirname(__file__))
 
static_url = '/static/'
 
static_root = os.path.join( site_root, 'static')
site_static_root = os.path.join( site_root, 'local_static')
 
# additional locations of static files
staticfiles_dirs = (
    # don't forget to use absolute paths, not relative paths.
    ('', site_static_root),
)
#************************************************************************************

2. 在 settings.py 同级目录下添加名称为 local_static 的目录

3.在local_static  目录中添加web.config 内容如下

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webserver>
    <!-- this configuration overrides the fastcgi handler to let iis serve the static files -->
    <handlers>
      <clear/>
      <add name="staticfile" path="*" verb="*" modules="staticfilemodule" resourcetype="file" requireaccess="read" />
    </handlers>
  </system.webserver>
</configuration>

最关键的一步,在项目根目录下面运行代码

python manage.py collectstatic

然后刷新看看,是不是成了?

卧薪尝胆饮咖啡~~