简体   繁体   English

Django:未加载静态文件

[英]Django : static files not loading

My django project contains a folder STATIC in which there are it's child folders css , images , js and swf .我的 django 项目包含一个文件夹STATIC ,其中有子文件夹cssimagesjsswf My web-site can not locate these files raising a 404 in the development server running on the terminal.我的网站无法在终端上运行的开发服务器中找到这些引发 404 错误的文件。

I have a settings_local.py file in which I have set the path such as (running on ubuntu 11.04)我有一个settings_local.py文件,我在其中设置了路径,例如(在 ubuntu 11.04 上运行)

STATIC_ROOT = '/home/zulaikha/dust.bin/my_project/static/'
STATIC_SERVE_PORT = 80

The sample settings-local.py file from the client suggest suggest so来自客户端的示例settings-local.py 文件建议如此

# static root
STATIC_ROOT = 'C:/Program Files (x86)/XAMPP/htdocs/static'
STATIC_SERVE_PORT = 8080

I have seen some similar issues but all regard STATICFILES which I read was introduced in Django 1.3 Where am I wrong and how can I correct myself?我见过一些类似的问题,但都认为我读到的STATICFILES是在 Django 1.3 中引入的,我哪里错了,我该如何纠正自己?

If you are indeed using django 1.2 then you must install django-staticfiles.如果您确实在使用 django 1.2,那么您必须安装 django-staticfiles。 If you do not see it in the included requirements.txt from your client then he either did not freeze it out into the file, or was not using that feature and instead was just pointing at it via apache or another production server.如果您在客户端包含的 requirements.txt 中没有看到它,那么他要么没有将其冻结到文件中,要么没有使用该功能,而是通过 apache 或其他生产服务器指向它。

Follow these instructions from the docs.按照文档中的这些说明进行操作。 Specifically the basic section: https://docs.djangoproject.com/en/dev/howto/static-files/特别是基本部分: https : //docs.djangoproject.com/en/dev/howto/static-files/

You will need to add staticfiles to your installed app.您需要将静态文件添加到已安装的应用程序中。 You should also not need to manually add a url since the django should do it automatically with the runserver command.您也不需要手动添加 url,因为 django 应该使用 runserver 命令自动添加。

Also verify that it works locally first by not running with sudo or a custom ip and port还要通过不使用 sudo 或自定义 ip 和端口运行来验证它是否首先在本地工作

According to your comments there are two options here:根据您的评论,这里有两个选项:

  1. Contact the programmer who wrote the code and ask him.联系写代码的程序员问他。
  2. Rewrite static serving logic with重写静态服务逻辑

In the simplest form, this is the solution to the problem.以最简单的形式,这就是问题的解决方案。 In project urls.py include the following lines在项目 urls.py 中包含以下几行

urlpatterns += patterns('',
    url(r'^static/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.STATIC_ROOT,
    }),

) )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM