【问题标题】:Django static files in Development not working开发中的 Django 静态文件不起作用
【发布时间】:2018-06-01 16:28:48
【问题描述】:

我的项目中有以下目录结构:

我正在尝试在我的环境中提供静态文件,但我总是收到 404 错误。

这是我的 settings.py 配置:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

我也把它放在我的 urls.py 中:

网址:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns  
urlpatterns += staticfiles_urlpatterns()

这是我的观点:

{% load static %}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <!-- Tell the browser to be responsive to screen width -->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <!-- Favicon icon -->
    <link rel="icon" type="image/png" sizes="16x16" href="../assets/images/favicon.png">
    <title>Nossas Finanças</title>
    <!-- Bootstrap Core CSS -->
    <link href="{% static 'plugins/bootstrap/css/bootstrap.min.css' %}" rel="stylesheet">

我已经学习了很多教程,但我不知道我做错了什么。我通过这个问题 (Django Static Files Development) 知道,静态文件在开发和生产中以两种不同的方式提供。

有人可以帮助我吗?

【问题讨论】:

标签: python django django-staticfiles


【解决方案1】:

试试这个

os.path.join(BASE_DIR, "finances", "static")

【讨论】:

  • 如果在路径的一部分硬编码分隔符,使用os.path.join() 有什么意义?这应该是os.path.join(BASE_DIR, "finances", "static")
  • 是的。做到了。 :)
  • @brunodesthuilliers,我认为直到STATICFILES_FINDERS 发生任何变化。由于两个查找器不同。
  • AppDirectoriesFinder 用于查找APPS。这里finances是他的项目
  • 呃!我显然需要另一杯咖啡#facepalm
【解决方案2】:

对于遇到此问题的其他人使用 Windows,请记住,您可能需要在添加新的静态文件后重新启动开发服务器(“.manage runserver”命令)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-08
    • 2016-08-20
    • 2012-08-16
    • 1970-01-01
    • 2012-01-14
    • 2012-02-29
    • 2015-11-09
    相关资源
    最近更新 更多