[29/May/2017 10:56:52] "GET /static/css/stylee.css HTTP/1.1" 404 1859 【附带解决方案】

今天遇到了一个棘手的问题,也非常奇怪。

出现了一个找不到css样式表的问题,之前配置好static路径之后也做了很小的测试;运行没有问题,css也能够正常加载。

问题描述 2017-05-29style.css文件的名字不能改变。仿佛系统只能识别style.css文件名。新建其他文件也不行,结果都是404错误,css文件不能正常加载。

我的环境是Django 1.10,目录和配置是按照官方Doc上来创建和配置的。

文件目录如下:

[29/May/2017 10:56:52]

 

setting.py:

STATIC_URL = '/static/'

STATIC_ROOT = [
    os.path.join(BASE_DIR, "static"),
    '/var/www/static/',
]

#common static file folder like jquery
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "common_static")
]

url.py:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^blogs/$', views.get_blogs),
    url(r'^detail/(\d+)/$', views.get_details ,name='blog_get_detail'),
    url(r'^login/$', views.blog_userLogin),
 ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


.html文件:(包括下面注释掉的代码,两种方式均已经验证)


    "UTF-8">
    My Blogs
    {% load static %}

   "stylesheet" type="text/css" href="/static/css/stylee.css">

 

而且在虚拟机下访问static路径的时候,只能访问到style.css文件。其他在static/css/下的文件都不能访问。

 [29/May/2017 10:56:52]

 

解决方案 2017-05-30:

  1. setting.py: STATIC_URL = '/static/' 为系统自带 ,其他的配置为新追加
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/
STATIC_URL = '/static/'

# MEDIA_ROOT = os.path.join(BASE_DIR, "media")
MEDIA_URL = '/media/'

# A list of locations of additional static files
STATICFILES_DIRS = (
 os.path.join(BASE_DIR, "static/js"),
)

# The default file storage backend used during the build process
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
 'django.contrib.staticfiles.finders.FileSystemFinder',
 'django.contrib.staticfiles.finders.AppDirectoriesFinder',
 # 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)

# Absolute path to the directory static files should be collected to.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = os.path.join( BASE_DIR, 'static')

2.urls.py:

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^blogs/$', views.get_blogs),
    url(r'^detail/(\d+)/$', views.get_details ,name='blog_get_detail'),
    url(r'^login/$', views.blog_userLogin),
 ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

3.目录结构(非常重要):

    • 大家如果看了官方的文档(如我上面的截图),它会告诉你在每个app下去建static文件夹.但是实际上按照我们配置的系统static_root都是在project目录下面。
    • 这就是为什么会出现我上面错误的原因,是我来来回回move文件夹造成的...懵逼了-_-!
    • 大家看我的工程为什么会有两个static folder,就是因为系统发布production环境的时候要执行 python manage.py collectstatic。来把每个app下面的static文件都拷贝到根static下面去。我下面有命令执行的截图。

[29/May/2017 10:56:52]

 

  • python manage.py collectstatic
[oracle@Ora11g myproject]$ python manage.py collectstatic

You have requested to collect static files at the destination
location as specified in your settings:

    /mnt/hgfs/database/myproject/static

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/icon-unknown-alt.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/icon-changelink.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/tooltag-arrowright.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/icon-deletelink.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/inline-delete.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/icon-alert.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/selector-icons.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/calendar-icons.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/README.txt'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/icon-unknown.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/LICENSE'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/icon-yes.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/icon-clock.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/tooltag-add.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/search.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/icon-addlink.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/sorting-icons.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/icon-calendar.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/icon-no.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/gis/move_vertex_off.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/img/gis/move_vertex_on.svg'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/core.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/jquery.init.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/urlify.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/actions.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/calendar.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/inlines.min.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/prepopulate.min.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/timeparse.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/cancel.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/SelectBox.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/change_form.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/popup_response.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/collapse.min.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/SelectFilter2.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/prepopulate.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/actions.min.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/collapse.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/inlines.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/prepopulate_init.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/admin/DateTimeShortcuts.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/vendor/jquery/jquery.min.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/vendor/jquery/LICENSE-JQUERY.txt'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/vendor/jquery/jquery.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/vendor/xregexp/xregexp.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/vendor/xregexp/xregexp.min.js'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/js/vendor/xregexp/LICENSE-XREGEXP.txt'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/css/rtl.css'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/css/fonts.css'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/css/base.css'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/css/login.css'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/css/forms.css'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/css/widgets.css'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/css/dashboard.css'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/css/changelists.css'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/fonts/Roboto-Regular-webfont.woff'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/fonts/Roboto-Bold-webfont.woff'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/fonts/LICENSE.txt'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/fonts/README.txt'
Copying '/usr/local/python3/lib/python3.5/site-packages/Django-1.10.7-py3.5.egg/django/contrib/admin/static/admin/fonts/Roboto-Light-webfont.woff'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/stylee.css'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/style_login.css'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/templatemo_style.css'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_body.jpg'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_comment.jpg'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_content_bottom.jpg'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_content_top.jpg'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_date.jpg'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_footer.jpg'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_footer_box.png'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_footer_list.png'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_image_01.jpg'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_image_02.jpg'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_image_03.jpg'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_list.jpg'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_logo.png'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_menu_left.jpg'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_menu_right.jpg'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_menu_wrapper.jpg'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_newsletter.png'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_rss.png'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_site_header.png'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_subscribe_btn.png'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_subscribe_input.png'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/templatemo_twitter.png'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/images_login/adm.png'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/images_login/avtar.png'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/images_login/bg1.jpg'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/images_login/close.png'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/images_login/key.png'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/images_login/pass.png'
Copying '/mnt/hgfs/database/myproject/blog/static/blog/images/images_login/Thumbs.db'

93 static files copied to '/mnt/hgfs/database/myproject/static'.
[oracle@Ora11g myproject]$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
May 29, 2017 - 15:42:57
Django version 1.10.7, using settings 'myproject.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
  •  好吧,好像经历了很多。

[29/May/2017 10:56:52]

 

转载于:https://www.cnblogs.com/porking/p/6918783.html

你可能感兴趣的:([29/May/2017 10:56:52] "GET /static/css/stylee.css HTTP/1.1" 404 1859 【附带解决方案】)