0

就像关于 Django 1.3 的第 10 亿个静态文件问题一样。我一直在寻找和尝试很多东西,但似乎没有什么对我有用。任何帮助,将不胜感激。将尝试提供尽可能多的信息。

网址文件

urlpatterns = patterns('',
url(r'^projectm/statictest/$','project_management.views.statictest'),)

看法

def statictest(request):
return render_to_response('statictest.html',locals())

模板

<html><head><title>Static Load Test Page</title></head>
<body>
{% load static %}
<img src="{{ STATIC_URL }}testimage.jpg" />
</body></html>

设置

STATIC_ROOT = '/home/baz/framework/mysite/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = ('',)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_DIRS = (
"/home/baz/framework/mysite/templates"

文件

-bash-3.2$ pwd
/home/baz/framework/mysite/templates
statictest.html

-bash-3.2$ pwd
/home/baz/framework/mysite/project_management/static
-bash-3.2$ ls
testimage.jpg

不太确定是否还有其他有用的信息。但基本上当我去这个测试页面并检查页面源时,url指向

<img src="/static/testimage.jpg" />

但是图像不加载。我也在多个浏览器上试过这个。也许我在某处遗漏了一个 imort 声明?

为帮助欢呼

4

2 回答 2

0

Are you using the built in runserver command or do you serve the Django app some other way?

If you use runserver then your problem is that you don't tell Django where to find your static assets in the filesystem. You need to set STATIC_ROOT to a filesystem path where your static assets can be found. Try setting it to: /home/baz/framework/mysite/project_management/static/

If you are using a different server (like gunicorn behind Nginx for example) then it is the responsibility of the front-end server to intercept requests for /static/ and serve them for you.

于 2013-02-18T01:59:29.523 回答
0

还记得在设置“STATIC_ROOT”后运行“collectstatic”管理命令。

https://docs.djangoproject.com/en/1.4/howto/static-files/#deploying-static-files-in-a-nutshell

于 2013-02-18T02:05:04.987 回答