【问题标题】:Static files on Django + Apache2 gives URLconf errorDjango + Apache2 上的静态文件给出 URLconf 错误
【发布时间】:2018-10-30 16:55:21
【问题描述】:

我正在尝试将下载的引导主题集成到我的实时网站中

我在 django 项目的根目录(manage.py 所在的位置)有一个 /static 目录,其中包含我所有的 css/javascript 文件。

来自我的 settings.py

STATIC_URL = '/static/'
STATICFILES_DIR = '/var/www/html/aerocredRoot/static'

在我的 index.html 中

{% load static %}
...href="{% static 'assets/web/.......' %}">

还有我的 urls.py

urlpatterns = [
    url(r'^%', views.index),
    url(r'^admin/', include(admin.site.urls)),
]

使用 Django 1.11.13。

在带有互联网浏览器的网络检查器上,我不断得到

Using the URLconf defined in Aerocred.urls, Django tried these URL 
patterns,in this order:

^$
^admin/
The current URL, static/assets/bootstrap/css/bootstrap.min.css, didn't match any 
of these.

我尝试将静态文件夹移动到我的应用程序的子目录,弄乱了 STATIC_ROOT 并做 collectstatic 的东西无济于事。

【问题讨论】:

    标签: django python-3.x ubuntu apache2 django-staticfiles


    【解决方案1】:

    我觉得应该是{% load staticfiles %}

    【讨论】:

    • 刚刚尝试,没有运气:(
    • 您之前是否在此项目中提供过静态文件?如果是这样,那么您的静态文件配置是可靠的。可能是文件路径不正确。
    【解决方案2】:

    请确保要加载的格式是 {% load static %} 而不是 (% load static %)。

    您是否运行 python manage.py collectstatic 将静态文件存储在 /var/www/html/aerocredRoot/static 中?

    【讨论】:

      【解决方案3】:

      所以我想通了,

      我需要遵循 Django 文档中解释的这个约定。所以我编辑了我的 urls.py 以匹配这个......

      from django.conf import settings
      from django.conf.urls.static import static
      
      urlpatterns = [
          # ... the rest of your URLconf goes here ...
      ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
      

      然后编辑 settings.py 来定义

      PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
      STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
      STATIC_URL = '/static/'
      STATICFILES_DIR = '/var/www/html/aerocredRoot/static'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-23
        • 1970-01-01
        • 2020-10-29
        • 2018-12-31
        • 2018-05-24
        • 2019-01-23
        • 1970-01-01
        相关资源
        最近更新 更多