简体   繁体   English

Django 1.11 - 找不到静态文件

[英]Django 1.11 - static file not found

I'm working on a Django project (Django 1.11), and I'm trying to use static file. 我正在研究Django项目(Django 1.11),我正在尝试使用静态文件。
This is my project structure: 这是我的项目结构:

|project_name
|---- app_name
|-------- src
|------------ static
|---------------- css
|-------------------- bootstrap
|------------------------ bootstrap.min.css
|-------- templates
|------------ base.html
|------------ first_template.html
|-------- views
|------------ first_view.py
|---- project_name
|-------- settings.py

In settings.py file, I have django.contrib.staticfiles in INSTALLED_APPS and I set STATIC_URL variable as follow: 在settings.py文件中,我在INSTALLED_APPS中有django.contrib.staticfiles ,我将STATIC_URL变量设置如下:

STATIC_URL = '/src/static/'

Then, I'd like use static files in base template, and this is what I've done: 然后,我想在基本模板中使用静态文件,这就是我所做的:

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>{% block title %}{% endblock %}</title>
    <link href="{% static "css/bootstrap/bootstrap.min.css" %}" rel="stylesheet">
</head>
<body>
    {% block content %}{% endblock content %}
</body>
</html>

When I load first_template.html , and so also the base.html file, the bootstrap.min.css file is not found (404). 当我加载first_template.html以及base.html文件时,找不到bootstrap.min.css文件(404)。

I know that is a trivial question, but i really don't understand what i'm missing. 我知道这是一个微不足道的问题,但我真的不明白我错过了什么。 I have checked a lot of similar SO questions without success, also because most of them refer to old django versions 我已经检查了很多类似的SO问题而没有成功,也因为他们中的大多数都是旧的django版本
Thank you in advance 先感谢您

Since you are using customised path, why dont you tell the django where to find it.. use this in settings and it shall work fine. 由于你使用自定义路径,为什么不告诉django在哪里找到它..在设置中使用它,它应该工作正常。

option 1: 选项1:

in case you are serious about preserving your file structure 如果您认真考虑保留文件结构

#settings.py
STATICFILES_DIRS = (
    ...
    os.path.join(BASE_DIR, 'app/src/static') # build appropriate path
)

UPDATE: 更新:

option 2: 选项2:

move the static folder to where manage.py exists or into the child of app. 将静态文件夹移动到manage.py所在的位置或移动到app的子级。

example: 例:

...manage.py
...static
...app/

or 要么

...manage.py
...app/static

STATIC_URL only control the prefix of static files URL (ie web address). STATIC_URL仅控制静态文件URL的前缀(即Web地址)。 It doesn't control their physical location on disk. 它不控制它们在磁盘上的物理位置。

Move your static files from app_name/src/static/ to app_name/static/ and that should fix it 将静态文件从app_name/src/static/app_name/static/ ,这应该修复它

Edit: If you want to keep directory structure as it is, you have couple of options 编辑:如果您想保持目录结构不变,您有几个选项

  1. Manually add your static file directories to STATICFILES_DIRS 手动将静态文件目录添加到STATICFILES_DIRS
  2. Create a class that inherit from django.contrib.staticfiles.finders.AppDirectoriesFinder and set the source_dir attribute 创建一个继承自django.contrib.staticfiles.finders.AppDirectoriesFinder的类并设置source_dir属性

from django.contrib.staticfiles.finders import AppDirectoriesFinder class MyAppDirFinder(AppDirectoriesFinder): source_dir = "src/static"

Then you can add your class to STATICFILES_FINDERS 然后,您可以将您的类添加到STATICFILES_FINDERS

STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'myapp.finders.MyAppDirFinder',

]

Reference: https://docs.djangoproject.com/en/1.11/ref/settings/#static-files 参考: https//docs.djangoproject.com/en/1.11/ref/settings/#static-files

Indside static folder you need to create a folder with app name Indside静态文件夹,您需要创建一个具有应用程序名称的文件夹

Now your folder structure is 现在您的文件夹结构是

   |-------- src
    |------------ static
    |---------------- css
    |-------------------- bootstrap
    |------------------------ bootstrap.min.css

Change it to 将其更改为

|-------- src
|------------ static
|-------------------app_name
|--------------------------- css
|-------------------- ----------bootstrap
|---------------------------------------- bootstrap.min.css

add

STATICFILES_DIRS=(os.path.join(BASE_DIR, '../frontend'),)

or 要么

STATICFILES_DIRS=()

in your settings. 在你的设置中。 Then it should work. 然后它应该工作。 Took me a long time to find this. 花了很长时间才找到这个。

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

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