python - Django STATIC_URL 不起作用,不提供文件

标签 python django

我在 http://example.com 上有一个公司 wordpress 博客,在 http://example.com/web 上成功部署了 Django 应用程序。

我的应用程序是一个简单的应用程序(我仍在学习 Django),我希望在其中为“欢迎”页面(以及其他页面)提供背景。

我在文档中读到我需要使用 STATIC_URL - 所以这是我所拥有的和我所做的:

  1. 我的项目结构:

enter image description here

  1. settings.py 文件中与提供静态文件相关的全部内容:
STATIC_URL = '/web/static/'
SETTINGS_PATH = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_PATH, os.pardir)
STATICFILES_DIRS = (os.path.join(PROJECT_PATH, "static"), )
USE_X_FORWARDED_HOST = True
FORCE_SCRIPT_NAME = '/web'
SUB_SITE = "/web"

这是模板文件 base.html,我想在其中使用 back.jpg 作为网页背景图片:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <title>Simple Site
        {% block title %}{% endblock %}</title>
    </head>
    <body background="/static/back.jpg" text="white"/>

                <div align="center">

        <h1>{% block head %}{% endblock %}</h1>
        {% block content %}{% endblock %}

                </div>

    </body>
</html>

但是,我看不到图像。这是一个生产(不是开发)应用程序。我该如何解决这个问题?

最佳答案

您的背景在错误的位置寻找背景图像,您应该使用 static template tag

<body background="{% static 'back.jpg' %}" text="white"/>    

要使用它,您需要静态加载(最好在模板顶部)

{% load static %}

关于python - Django STATIC_URL 不起作用,不提供文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31873961/

相关文章:

python - 混淆循环问题(python)

python - nginx+uwsgi+django,uwsgi中好像有什么奇怪的缓存,帮帮我

django - Tornado 和 Django 的限制?

python - 如何检查python模块是否存在并且可以导入

python - 使用 Python 将 JSON 嵌套到扁平化 JSON

python - 如何发送 Elasticsearch 查询结果的响应API?

python - 不断收到 Kattis 问题的运行时错误

javascript - 从ajax POST方法获取数据后,在django中打开另一个窗口

python - 在 Django 中使用 Sendgrid 发送电子邮件

python - 如何在 django python 中通过子进程运行 unoconv?