尝试在开发服务器上提供django静态文件 - 未找到 [英] Trying to serve django static files on development server - not found

查看:100
本文介绍了尝试在开发服务器上提供django静态文件 - 未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已按照此问题 文档,我甚至看过这个,但到目前为止,我无法使用 python manage.py runserver 获取静态文件。



这些是我的(相关的)设置:

  STATIC_ROOT'/ home / wayne / programming / somesite / static'
STATICFILES_DIRS('/ home / wayne / programming / somesite / static / styles',
'/ home / wayne / programming / somesite / static / / code>

在我的urls.py:

  from django.contrib.staticfiles.urls import staticfiles_urlpatterns 
#我的其他URL在这里...
如果settings.DEBUG:
urlpatterns + = staticfiles_urlpatterns()

我h在我的base.html

 < link rel =stylesheettype =text / csshref = {%staticstyles / main.css%}> 
{%block styles%}
{%for styles in styles%}
< link rel =stylesheettype =text / csshref ={%static sheet% >
{%endfor%}

我保证我不会幻觉:

 (。env)wayne:〜/ programming / somesite / static $ pwd 
/ home / wayne / programming / somesite / static
(.env)wayne:〜/ programming / somesite / static $ ls
管理样式

但是,导航到 http:// localhost:8000 / 我的网站缺少样式表,当我去 http: //localhost:8000/static/styles/main.css 我得到'styles / main.css'找不到,并尝试导航到 localhost:8000 / static / localhost:8000 / static / styles ,它告诉我目录索引不允许。



我在这里缺少什么?

解决方案

Django's处理静态文件仍然会有点混乱,特别是在相关设置的命名方面。



简单的答案是移动你的静态文件ES;而不是

  / home / wayne / programming / somesite / static 

将它们放在

  / home / wayne / programming / somesite / yourapp / static 

(其中 yourapp显然是您的主要应用程序的名称)。



较长的答案是,我认为(可以理解)对各种设置感到困惑。 STATIC_ROOT 指运行管理后,您的静态文件应该最终的位置。 py collectstatic 。你不需要这个集合(因为你在开发本地时不应该真正需要 collectstatic )。无论哪种方式, STATIC_ROOT 应该始终引用一个空目录。



您的 STATICFILES_DIRS 设置几乎可以工作,除了你已经告诉Django有两个路径应该找到静态文件

  / home / wayne / programming / somesite / static / styles 
/ home / wayne / programming / somesite / static / admin

所以当你做 {%staticstyles / main.css%} 它会寻找



  /home/wayne/programming/somesite/static/styles/styles/main.css 
/home/wayne/programming/somesite/static/admin/styles/main.css

并将显然找不到他们。

  STATICFILES_DIRS =('/ home / wayne / programming / somesite / static' ,)

但是没有必要这样做,因为你可以依靠 django.contrib.staticfiles.finders.AppDirectoriesFinder (在默认的STATICFILES_FINDERS ),并将静态文件移动到一个应用程序目录。



希望这样可以清理一些。 p>

I've followed the instructions in this question, the documentation, and I've even looked at this one, but so far I'm unable to get at my static files using python manage.py runserver.

These are my (relevant?) settings:

STATIC_ROOT '/home/wayne/programming/somesite/static'
STATICFILES_DIRS ('/home/wayne/programming/somesite/static/styles',
                  '/home/wayne/programming/somesite/static/admin')

In my urls.py:

from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# The rest of my urls here...
if settings.DEBUG:
    urlpatterns += staticfiles_urlpatterns()

I have the following code in my base.html

    <link rel="stylesheet" type="text/css" href="{% static "styles/main.css" %}">
    {% block styles %}
    {% for sheet in styles %}
    <link rel="stylesheet" type="text/css" href="{% static sheet %}">
    {% endfor %}

And I promise I'm not hallucinating:

(.env)wayne:~/programming/somesite/static$ pwd 
/home/wayne/programming/somesite/static 
(.env)wayne:~/programming/somesite/static$ ls 
admin  styles 

However, when navigate to http://localhost:8000/ my site is missing its stylesheets, and when I go to http://localhost:8000/static/styles/main.css I get 'styles/main.css' could not be found, and trying to navigate to localhost:8000/static/, or localhost:8000/static/styles it tells me that Directory indexes are not allowed.

What am I missing here?

解决方案

Django's handling of static files continue to be slightly confusing, particularly in terms of the naming of relevant settings.

The short answer is to move your static files; instead of

/home/wayne/programming/somesite/static

put them in

/home/wayne/programming/somesite/yourapp/static

(where "yourapp" is obviously the name of your main application).

The longer answer is that I think you've (understandably) become confused about the various settings. STATIC_ROOT only refers to the location where your static files should end up after running manage.py collectstatic. You don't need this set (as you shouldn't really need collectstatic) when developing locally. Either way, STATIC_ROOT should always refer to an empty directory.

Your STATICFILES_DIRS setting would almost work, except that you've told Django there are two paths where it should find static files

/home/wayne/programming/somesite/static/styles
/home/wayne/programming/somesite/static/admin

so when you do {% static "styles/main.css" %} it will look for

/home/wayne/programming/somesite/static/styles/styles/main.css
/home/wayne/programming/somesite/static/admin/styles/main.css

and will obviously not find them. What might work is

STATICFILES_DIRS = ('/home/wayne/programming/somesite/static',)

but there's no need to do that, as you can just rely on django.contrib.staticfiles.finders.AppDirectoriesFinder (in the default STATICFILES_FINDERS) and move your static files to an app directory.

Hope this clears things up a little.

这篇关于尝试在开发服务器上提供django静态文件 - 未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆