Django - 找不到静态图片

发布于 2024-12-20 22:11:55 字数 544 浏览 2 评论 0 原文

大家好,

我正在开发模式下运行 Django 应用程序。我已将静态文件收集到项目中的 /static/images/ 目录中。

在我的模板中,我尝试链接示例图像:(

<img src="{{ STATIC_URL }}items/no_image.jpeg"/>
    {{ STATIC_URL }}items/no_image.jpeg

底线打印用于调试目的)

图片显示链接损坏,底线打印出正确的目录:

 /static/items/no_image.jpeg

在我的项目中,我确实有 /static/ items/no_image.jpeg 文件。

在我的 settings.py 中,我有:

STATIC_ROOT = os.path.dirname(__file__)+'/static/'
STATIC_URL = '/static/'

有人可以帮忙吗?

谢谢 !

Helo all,

I am running a Django application in development mode. I have collected static files into a /static/images/ directory in my project.

In my template I try to link an example image:

<img src="{{ STATIC_URL }}items/no_image.jpeg"/>
    {{ STATIC_URL }}items/no_image.jpeg

(The bottom line is printing for debug purposes)

The picture shows with a broken link, and the bottom line prints out the correct directory:

 /static/items/no_image.jpeg

Inside my project, I do have the /static/items/no_image.jpeg file.

In my settings.py I have:

STATIC_ROOT = os.path.dirname(__file__)+'/static/'
STATIC_URL = '/static/'

Can anybody help?

Thank you !

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

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

发布评论

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

评论(4

攀登最高峰 2024-12-27 22:11:55

发现问题了。问题是我使用了错误的目录。 STATIC_URL 被命名为 /site_media/,而它应该是 /static/。改变了它,现在一切都像水一样清晰......

Found out the problem. The problem was I was using the wrong directory. STATIC_URL was named /site_media/ when it should be /static/. Changed it and everything now works clear as water...

北城挽邺 2024-12-27 22:11:55

我认为这是斜线问题。尝试

STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static')

I think it's slash issue. Try

STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static')
女皇必胜 2024-12-27 22:11:55

也许你对静态文件的收集有误解。在开发过程中无需使用collectstatic 命令将它们收集到目标目录中。这是为了部署而完成的。

在开发期间,您只需添加 django.contrib.staticfiles 应用程序,然后按照所述指定 STATIC_ROOT、STATIC_URL 和 STATICFILES_DIRS 此处

然后,在您的基本 urlconf 中,您需要添加静态文件的 url 规则,如下所述 此处。这就是“开发模式”。

在生产模式下,您首先运行collectstatic命令,然后将生成的目录推送到运行apache(或nginx或其他)的服务器,并让他在www.yoururl下提供该目录。 com/static/

关于在 django 中处理静态文件的整个故事几乎让我发疯……即使在今天,当我试图记住它或解释它时,我也必须思考几分钟。 :-/ 如果有时它让您感到困惑,请不要担心。

Maybe you've misunderstood the collecting of static files. There's no need to collect them into a target directory with the collectstatic command during development. That's meant to be done for deployment.

During dev, you only add the django.contrib.staticfiles app, then specify the STATIC_ROOT, STATIC_URL and STATICFILES_DIRS as described here.

Then, in your base urlconf you need to add the url rules for the staticfiles as described here. That's it for "development mode".

In production mode, you run the collectstatic command first, then shove the resulting directory over to the server where your apache (or nginx or whatever) is running, and let him serve that directory unter www.yoururl.com/static/

The whole story about handling staticfiles in django almost drove me nuts ... and even today I have to think for some minutes when trying to remember it or explain it. :-/ Don't worry if it confuses you sometimes.

追我者格杀勿论 2024-12-27 22:11:55

我想我最近也遇到了类似的问题。尝试将图像直接放置在 /static/ 而不是 /static/items/ 中。如果您希望能够直接链接到 /static/items/sample.jpg 您需要将 /static/items/ 添加到 SETTINGS 中的 STATIC_ROOT

I think I recently had a similar issue. Try placing your images directly in /static/ instead of /static/items/. If you want to be able to directly link to /static/items/sample.jpg you need to add /static/items/ to your STATIC_ROOT in SETTINGS

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