2017-03-04 166 views
0

settings.py中,我的部分代码设置了静态url。Django不会服务我的CSS或Javascript文件 - 静态文件

STATIC_ROOT = os.path.join(BASE_DIR, "static") 
STATIC_URL = '/static/' 

我的静态文件和文件夹位于/home/myLinuxUsername/myApp/static/interactive-preview-dependencies。这包含CSS,JS和图像的文件夹。它有文件夹images,scriptsstylesheets

urls.pyurlpatterns是这样的:

urlpatterns = [ 
    # .. SOME CODE HERE .. 
    url(r'^interactive/$', interactive), 
] 

views.py是这样的:

def interactive(request): 
    t= get_template('interactive/index.html') 
    return HttpResponse(t.render()) 

下面是我如何尝试在加载CSS的例子interactive/index.html(更新)

<link rel="stylesheet" href=static 'interactive-preview-dependencies/styles/main.css' %}"> 

当我运行python manage.py runserver和去localhost:8000/interactive,终端给了我这个错误:"GET /static/interactive-preview-dependencies/styles/main.css HTTP/1.1" 404 1757

如何解决这个问题,从而Django的查找和加载CSS?

回答

1

如果我理解正确,您的应用程序被称为myAppinteractive-preview-dependencies只是/home/myLinuxUsername/myApp/static/中的子文件夹。根据Managing static files。你应该引用它作为

... href="{% static "myApp/interactive-preview-dependencies/styles/main.css" %}" 
+0

这将如何工作,如果我想这方面的工作在不同的机器,尤其是在Mac或Windows上?绝对路径每次不会相同 – Username

+0

应该没关系。我的意思是,你应该添加应用程序名称的网址。 – fodma1

+0

啊我明白了。我编辑了相关的代码,但是我得到了这个404错误:'“GET /static/myApp/interactive-preview-dependencies/styles/main.css HTTP/1.1”404 1781'。看起来像Django现在正在寻找一个不同的地方... – Username