Django:静态文件未加载

发布于 2024-12-21 20:59:40 字数 620 浏览 2 评论 0原文

我的 django 项目包含一个文件夹 STATIC,其中有它的子文件夹 cssimagesjs>swf。我的网站无法找到这些文件,从而在终端上运行的开发服务器中引发 404 错误。

我有一个 settings_local.py 文件,其中我设置了路径,例如(在 ubuntu 11.04 上运行)

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

来自客户端的 sample settings-local.py 文件建议建议如此

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

我见过一些类似的问题,但都考虑到我读到的 STATICFILES 是在 Django 1.3 中引入的 我哪里错了,我该如何纠正自己?

My django project contains a folder STATIC in which there are it's child folders css, images, js and swf. My web-site can not locate these files raising a 404 in the development server running on the terminal.

I have a settings_local.py file in which I have set the path such as (running on 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁对谁错谁最难过 2024-12-28 20:59:40

如果您确实使用 django 1.2,那么您必须安装 django-staticfiles。如果您在客户端包含的 requests.txt 中没有看到它,那么他要么没有将其冻结到文件中,要么没有使用该功能,而只是通过 apache 或其他生产服务器指向它。

请遵循文档中的这些说明。具体来说,基本部分:
https://docs.djangoproject.com/en/dev/howto/static- files/

您需要将静态文件添加到已安装的应用程序中。您也不应该需要手动添加 url,因为 django 应该使用 runserver 命令自动执行此操作。

还要首先通过不使用 sudo 或自定义 IP 和端口运行来验证它是否在本地工作

If you are indeed using django 1.2 then you must install 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.

Follow these instructions from the docs. Specifically the basic section:
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.

Also verify that it works locally first by not running with sudo or a custom ip and port

燕归巢 2024-12-28 20:59:40

根据您的评论,这里有两个选择:

  1. 联系编写代码的程序员并询问他。
  2. 重写静态服务逻辑

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
怎樣才叫好 2024-12-28 20:59:40

以最简单的形式,这就是问题的解决方案。在项目 urls.py 中包含以下几行

urlpatterns += patterns('',
    url(r'^static/(?P<path>.*)

, 'django.views.static.serve', { 'document_root': settings.STATIC_ROOT, }),

In the simplest form, this is the solution to the problem. In project urls.py include the following lines

urlpatterns += patterns('',
    url(r'^static/(?P<path>.*)

)

, 'django.views.static.serve', { 'document_root': settings.STATIC_ROOT, }),

)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文