【问题标题】:static files not found in django 1.11在 django 1.11 中找不到静态文件
【发布时间】:2019-04-26 18:04:05
【问题描述】:

访问本地主机上的任何页面时,我都会收到"Not Found"。 我已经阅读了所有相关的帖子,但仍然不明白为什么。

我已将'django.contrib.staticfiles' 包含在 INSTALLED_APPS 中。

我的 settings.py 中有DEBUG = TRUE

我的项目布局和代码如下。

提前谢谢你。

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

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

<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

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

views.py

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

【问题讨论】:

    标签: python django path django-staticfiles static-files


    【解决方案1】:

    我没有,

    {% load static %}

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

    添加这些解决了问题。

    谢谢!

    【讨论】:

    • 太好了,你解决了它。如果有帮助,接受答案或投票会很好。
    【解决方案2】:

    您的 STATICFILES_DIRS 定义不正确。您的 BASE_DIR 很可能被定义为您的 project_root 文件夹,因为这是默认的 Django 设置。您应该在定义中省略 project_root。

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

    您还应该在 urlpatterns 中包含静态文件 url。

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

    【讨论】:

    • 我已尝试按照建议更改 STATICFILES_DIRS,但在运行 python manage.py findstatic 时无法找到任何静态文件。
    • BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    猜你喜欢
    • 2018-11-15
    • 2018-04-09
    • 1970-01-01
    • 2021-08-27
    • 2014-11-10
    • 2011-08-26
    • 2020-06-22
    • 2021-08-16
    相关资源
    最近更新 更多