如何使用django在IIS上获取静态CSS文件? [英] How to fetch static CSS files with django on IIS?

查看:373
本文介绍了如何使用django在IIS上获取静态CSS文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有django安装在IIS8上。现在我想配置它来提供静态文件。我一直在追踪



解决方案



1)在静态/目录中,创建一个名为 web.config 的新文件(请注意,您可能在上层目录中有另一个 web.config 文件; t触摸它,只要离开它)。



2)将以下内容写入此文件的内容:

 <?xml version =1.0encoding =UTF-8?> 
< configuration>
< system.webServer>
<! - 此配置覆盖FastCGI处理程序,让IIS提供静态文件 - >
<处理程序>
< clear />
< add name =StaticFilepath =*verb =*modules =StaticFileModuleresourceType =FilerequireAccess =Read/>
< / handlers>
< /system.webServer>
< / configuration>

3)转到您的IIS服务器 - >右键单击站点名称 - >添加虚拟目录



4)在别名中写static(这是必须)。然后,导航到项目中所有静态文件的文件夹





5)运行IIS服务器并进入您的网站。它应该工作。


I have django installed on IIS8. Now I want to configure it to serve static files. I have been following this tutorial

I have added this to settings.py

STATIC_URL = '/static/'

STATIC_ROOT = 'C:/my_django_project/NZtracker/static_for_production/'

STATICFILES_DIRS = ( 'C:/my_django_project/NZtracker/static/', )

and then run collectstatic.

As a result, all the static folders have been moved to C:/my_django_project/NZtracker/static_for_production/.

Next, I was trying to configure IIS to serve files from that folder but couldn't. (I have tried following this post, but it also did not work)

How can I fix it? Thanks

解决方案

Yes, I am a newbie. Sometimes I ask "newbie questions", same as many others here. But apparently, there is no room for such questions on this site (and this is why I got down-voted on my initial question at the top and got banned out of this site - this is my last answer that I can write on this site). Guys, people ask questions because they honestly ask for help and hope to get it here. They don't do it to irritate any of you (you can vote to close this answer too, if it makes you feel good)


Anyway, after struggling for 2 days, for the sake of those who have the same problem, I wanted to share a step-by-step solution for (probably a common) issue.

Problem

You have started a django project on IIS and it is working perfectly on your localhost. Now, when deploying it to web-server, the static files (CSS, JS, images,..) are not fetched. You can read here and here , but in fact, you don't want all these configurations and copying files from one directory to another...

What you want is that your IIS server will fetch the static files from the static/ directory, just as the development server (the one you run with python manage.py runserver) did.

Solution

1) inside the static/ directory, create a new file called web.config (notice, you probably have another web.config file in upper directory - don;t touch it. just leave it as is).

2) Write the following as the content of this file:

<?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>

3) Go to your IIS server -> right click the site name -> Add virtual directory

4) in alias wrote "static" (This is a MUST). Then, navigate to the folder in the project where all the static files are

5) run IIS server and enter your website. It should work.

这篇关于如何使用django在IIS上获取静态CSS文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆