简体   繁体   English

在Django 1.11中找不到静态文件

[英]static files not found in django 1.11

I am getting "Not Found" when accessing any page on localhost. 访问本地主机上的任何页面时,都收到“找不到”消息 I have read all related posts but still cannot understand why. 我已经阅读了所有相关文章,但仍然不明白为什么。

I have included 'django.contrib.staticfiles' in INSTALLED_APPS. 我在INSTALLED_APPS中包含了'django.contrib.staticfiles'

I have DEBUG = TRUE in my settings.py 我的settings.py中有DEBUG = TRUE

my project layout and code is as the following. 我的项目布局和代码如下。

Thank you in advance. 先感谢您。

repo_root
├── manage.py
│   
├── project_root
│   │
│   ├── accounts
│   │   ├── migrations
│   │   ├── __init__.py
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── models.py
│   │   ├── tests.py
│   │   ├── views.py
│   │   └── urls.py
│   │
│   ├── templates
│   │   └── index.html   
│   │
│   └── static
│       └── assets
│           ├── bootstrap
│           ├── css
│           ├── fonts
│           ├── img
│           └── js
│   
├── config_root
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
│  

settings.py settings.py

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(
        BASE_DIR,
        'project_root/static/assets')
]

STATIC_ROOT = os.path.join(
    BASE_DIR,
    'project_root/staticfiles')

index.html 的index.html

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
    <title>index</title>
    <meta name="description" content="sample_content">
    <link rel="stylesheet" href="'static/assets/bootstrap/css/bootstrap.min.css'">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat:400,400i,700,700i,600,600i">
    <link rel="stylesheet" href="'static/assets/fonts/simple-line-icons.min.css'">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/baguettebox.js/1.10.0/baguetteBox.min.css">
    <link rel="stylesheet" href="'static/assets/css/smoothproducts.css'">
</head>

urls.py urls.py

url(r'^index/$', IndexView.as_view())

views.py views.py

class IndexView(TemplateView):
    template_name = "index.html"

I didn't have, 我没有

{% load static %}

and

href="{% static 'assets/bootstrap/css/bootstrap.min.css' %}"

adding these solved the problem. 添加这些解决了问题。

Thank you! 谢谢!

Your STATICFILES_DIRS isn't defined properly. 您的STATICFILES_DIRS定义不正确。 Your BASE_DIR is most probably defined as your project_root folder, since this is the default Django setting. 您的BASE_DIR很可能定义为project_root文件夹,因为这是默认的Django设置。 You should leave out project_root in your definition. 您应该在定义中忽略project_root。

STATICFILES_DIRS = [ os.path.join( BASE_DIR, '/static/') ]

You should also include static files urls to your urlpatterns. 您还应在urlpatterns中包含静态文件url。

Check Django docs for further info https://docs.djangoproject.com/en/2.1/howto/static-files/ 检查Django文档以获取更多信息https://docs.djangoproject.com/en/2.1/howto/static-files/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM