【问题标题】:django static files do not work for one application onlydjango 静态文件仅适用于一个应用程序
【发布时间】:2014-01-07 09:05:49
【问题描述】:

我的 django 应用程序运行良好,除了一个应用程序,所有静态文件都没有加载。

这发生在这个网址上:http://localhost/db_mgmt/add/dg/。模板已加载,但没有css,没有js。 当我查看其中一个错误时,浏览器会尝试加载页面http://localhost/db_mgmt/add/static/jquery.min.js,但链接应该是:http://localhost/static/jquery.min.js 这些文件在 base.html 中加载并在其他任何地方工作......

base.html 中包含的示例:

<script type="text/javascript" src="{{ STATIC_URL }}jquery.min.js"></script>

如果有帮助,这是我的 settings.py:

# Django settings for europolix project.
from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCP

import os
#root of the project
PROJECT_ROOT=os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
#root of templates
TEMPLATE_ROOT=os.path.join(PROJECT_ROOT, 'templates')
#root of media files (import / export files)
MEDIA_ROOT=os.path.join(PROJECT_ROOT, 'media')
#root of static files (css, js, jquery...)
STATIC_ROOT=os.path.join(PROJECT_ROOT, 'static')
#WEB_ROOT=url of WSGIScriptAlias given in the apache configuration file (/etc/apache2/apache2.conf in Lubuntu 13.04)
#example: WSGIScriptAlias /europolix /var/www/europolix/europolix/wsgi.py -> WEB_ROOT="/europolix"
#in the apache configuration file, you must update the alias for static files as well
#ex: Alias /europolix/static /var/www/europolix/static -> WEB_ROOT="/europolix"
WEB_ROOT="/europolix"
#local
WEB_ROOT=".."
# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash.
# Examples: "http://media.actrence.com/media/", "http://example.com/media/"
MEDIA_URL=WEB_ROOT+'/media/'

# URL prefix for static files.
# Example: "http://media.actrence.com/static/"
STATIC_URL=WEB_ROOT+'/static/'

# Additional locations of static files

STATICFILES_DIRS=(
    STATIC_ROOT,
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

TEMPLATE_DIRS=(
    TEMPLATE_ROOT
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

如果我在视图中打印这些变量,我有以下路径:

PROJECT_ROOT /var/www/europolix
STATIC_ROOT /var/www/europolix/static
STATIC_URL ../static/
WEB_ROOT ..

提前感谢您的帮助。

编辑:

变量 WEB_ROOT 在本地是相对的。会不会是问题? 变量在父目录中搜索(对吗?)。因此,url 中只有一个“孩子”的应用程序(http://localhost/acthttp://localhost/export)可以工作,但有很多“孩子”的应用程序不能工作(例如:上面的那个,http://localhost/db_mgmt/add/dg/

导出应用的urls.py:

urlpatterns=patterns('export.views',
    url(r'^/?$', 'export', name='export'),
)

db_mgmt 应用程序的 urls.py 不能正常工作:

urlpatterns=patterns('db_mgmt.views',
    url(r'^add/(?P<field>\w+)/$', 'add', name='add'),
    url(r'^form_add.html/(?P<field>\w+)/$', 'form_add', name='form_add'),
)

【问题讨论】:

    标签: django path settings django-staticfiles


    【解决方案1】:

    我们需要更多地了解可以运行的应用程序和给您带来麻烦的应用程序之间的区别

    【讨论】:

      【解决方案2】:

      正如我所想,问题之一与 WEB_ROOT 变量有关。 这是我修复它的方法:

      WEB_ROOT="http://127.0.0.1:8000"
      

      另一个问题是我的应用程序 db_mgmt 的模板路径:

      url="/db_mgmt/form_add.html/"+field+"/"
      

      我必须删除第一个斜线才能让它工作:

      url="db_mgmt/form_add.html/"+field+"/"
      

      已解决:)。

      【讨论】:

        猜你喜欢
        • 2019-01-25
        • 1970-01-01
        • 2017-06-15
        • 1970-01-01
        • 2011-06-01
        • 2011-10-24
        • 2016-04-06
        • 2018-08-22
        • 1970-01-01
        相关资源
        最近更新 更多