在Django模板中如何加载静态文件?

huangapple go评论64阅读模式
英文:

How to Loading Static Files in Django Template?

问题

问题描述:
我正在尝试在我的Django项目中设置一个目录结构,其中static文件夹位于user文件夹内,而user文件夹又位于templates文件夹内。我的意图是让静态文件靠近模板文件。然而,我在让Django从这个目录结构中正确提供静态文件方面遇到了困难。

目录结构:

project/
├── templates/
│   └── user/
│       ├── index.html
│       └── static/
│           └── css/
│               └── main.css
└── ...

其他细节:

  • 我在settings.py中配置了STATIC_URL
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR,'/static/')]
STATIC_ROOT = os.path.join(BASE_DIR, "/static")
  • 在我的index.html文件中,我使用<link rel="stylesheet" href="{% static 'css/main.css' %}">标签来引用静态文件。

遇到的问题:
尽管调整了设置,但我仍然无法使Django从这个特定的目录结构中正确提供静态文件。我尝试了不同的配置,比如更改STATIC_URL,但仍然没有成功。非常感谢任何帮助。

英文:

Problem Description:
I'm trying to set up a directory structure in my Django project where the static folder is inside the user folder, which is in turn inside the templates folder. My intention is to have static files located close to the template files. However, I'm having difficulties getting Django to properly serve static files from this directory structure.

Directory Structure:

project/
├── templates/
│   └── user/
│       ├── index.html
│       └── static/
│           └── css/
│               └── main.css
└── ...

Additional Details:

  • I've configured STATIC_URL in settings.py:
STATIC_URL = &#39;/static/&#39;
STATICFILES_DIRS = [os.path.join(BASE_DIR,&#39;/static/&#39;)]
STATIC_ROOT = os.path.join(BASE_DIR, &quot;/static&quot;)
  • In my index.html file, I'm using the &lt;link rel=&quot;stylesheet&quot; href=&quot;{% static &#39;css/main.css&#39; %}&quot;&gt; tag to reference static files.

Issue Faced:
Despite adjusting the settings, I'm unable to make Django serve static files properly from this specific directory structure. I've tried different configurations, such as changing STATIC_URL, but I still haven't succeeded. Any help would be greatly appreciated.

答案1

得分: 1

解决方案:

  1. 更新STATICFILES_DIRSsettings.py文件中,用户最初使用路径'C:/user/templates/user/static'配置了STATICFILES_DIRS。然而,这个路径结构是不正确的。

  2. 移除前导斜杠: 用户在os.path.join调用中移除了'user/templates/user/static'之前的前导斜杠,以建立正确的路径。

    STATICFILES_DIRS = [os.path.join(BASE_DIR, 'user/templates/user/static')]
    
  3. 使用正斜杠: 用户确保在目录路径中使用正斜杠/,即使在Windows系统上也是如此,因为Python和Django在内部处理路径转换。

  4. 重新启动服务器: 应用这些更改后,用户重新启动了Django开发服务器以生效配置。

英文:

Solution:

  1. Updating STATICFILES_DIRS: In the settings.py file, the user had initially configured STATICFILES_DIRS with the path &#39;C:/user/templates/user/static&#39;. However, this path structure was incorrect.

  2. Removing Leading Slash: The user removed the leading slash before &#39;user/templates/user/static&#39; in the os.path.join call to establish a proper path.

    STATICFILES_DIRS = [os.path.join(BASE_DIR, &#39;user/templates/user/static&#39;)]
    
  3. Using Forward Slashes: The user ensured the use of forward slashes / in the directory path, even on Windows systems, as Python and Django internally handle path conversions.

  4. Restarting Server: After applying these changes, the user restarted the Django development server to enact the configuration.


huangapple
  • 本文由 发表于 2023年8月8日 21:08:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定