简体   繁体   English

如何在Django项目中使用静态文件?

[英]How can I get static files to work in my Django project?

Using: Python 3.7 and Django 2.1.4 使用:Python 3.7和Django 2.1.4

I'm working through the tutorials in William S. Vincent's Django for Beginners book. 我正在研究William S.Vincent的Django入门指南中的教程。 I'm in the "Blog App" part of the book, and I've been following his instructions to the letter. 我在本书的“博客应用”部分,并且一直遵循他对这封信的指示。 However, I cannot seem to get the dev server to serve my static CSS file, no matter what I do. 但是,无论我做什么,我似乎都无法让开发服务器来服务我的静态CSS文件。

My source code can be found here. 我的源代码可以在这里找到。

I'm using manage.py runserver to run a development demo server for local testing. 我正在使用manage.py runserver运行开发演示服务器以进行本地测试。

Here's my directory tree: 这是我的目录树:

./
├── blog
│   ├── admin.py
│   ├── apps.py
│   ├── __init__.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── blog_project
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── db.sqlite3
├── manage.py
├── Pipfile
├── Pipfile.lock
├── static
│   └── css
│       └── base.css
└── templates
    ├── base.html
    └── home.html

Originally I updated my blog_project/settings.py with this line: 最初,我用以下代码行更新了blog_project/settings.py

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

and I put {% load static %} at the top of my templates/base.html file, as well as adding <link href="{% static 'css/base.css' %}" rel="stylesheet"> in the <head> ... </head> section. 然后在templates/base.html文件的顶部放置{% load static %} ,并在其中添加<link href="{% static 'css/base.css' %}" rel="stylesheet"> <head> ... </head>部分。

After writing the static/css/base.css file, this should have worked. 写入static/css/base.css文件后,此方法应该可以正常工作。 But it didn't. 但事实并非如此。 I researched the forums and Stack Overflow, and saw people advising adding the static directories to the blog_project/settings.py file, so I updated that file, adding the following to the bottom: 我研究了论坛和Stack Overflow,看到有人建议将静态目录添加到blog_project/settings.py文件中,因此我更新了该文件,并在底部添加了以下内容:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.1/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICTILES_DIRS = [
    STATIC_ROOT,
]

Then I updated the blog_project/urls.py file, adding two new imports: 然后,我更新了blog_project/urls.py文件,添加了两个新的导入:

from django.conf.urls.static import static
from django.conf import settings

and the following line: 和以下行:

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

But even this didn't work. 但是,即使这样也没有用。

You can check my settings.py , urls.py and base.html files to see what I'm talking about. 您可以检查我的settings.pyurls.pybase.html文件,以了解我在说什么。

I expected the manage.py runserver to host the static files, but it did not. 我希望manage.py runserver可以托管静态文件,但事实并非如此。 When I tried to load the CSS file directly, I got a 404 error. 当我尝试直接加载CSS文件时,出现404错误。

First off, you misspelled your STATICFILES_DIRS in the linked settings.py file with a rampant T instead of an F: STATIC T ILES_DIRS . 首先,您在链接的settings.py文件中使用横行的T而不是F来拼写错误的STATICFILES_DIRSSTATIC T ILES_DIRS

Second, do not put your STATIC_ROOT anywhere within your STATICFILES_DIRS . 其次,不要把你的STATIC_ROOT任何地方你的内STATICFILES_DIRS From the docs: 从文档:

Warning 警告

This ( STATIC_ROOT ) should be an initially empty destination directory for collecting your static files from their permanent locations into one directory for ease of deployment; 这个( STATIC_ROOT )应该是一个最初为空的目标目录,用于将静态文件从其永久位置收集到一个目录中,以便于部署; it is not a place to store your static files permanently. 它不是永久存储静态文件的地方。 You should do that ( by that they mean 'store your static files' ) in directories that will be found by staticfiles's finders, which by default, are 'static/' app sub-directories and any directories you include in STATICFILES_DIRS ). 您应该在静态文件的查找器将找到的目录中执行此操作( 这意味着它们“存储您的静态文件” ),默认情况下,这些目录为“ static /”应用子目录以及您包括在STATICFILES_DIRS任何目录)。

Note: cursive text was added by me. 注意:草书是我添加的。


So I recommend the following configuration for your setttings.py: 因此,我建议您的setttings.py使用以下配置:

STATIC_ROOT = os.path.join(BASE_DIR, 'static_files')
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

As long as you are in debugging mode, DEBUG=True , you should be good to go. 只要您处于调试模式DEBUG=True ,您就应该很好。 There is also no need to add the STATIC_ROOT to the urlpatterns as you did in your urls.py . 也无需像在urls.py那样将urlpatterns添加到STATIC_ROOT中。 You only want to do that if you don't have django.contrib.staticfiles in your INSTALLED_APPS (see here ). 如果您的INSTALLED_APPS没有django.contrib.staticfiles ,则只想这样做(请参阅此处 )。

At the moment you want to switch to production, have a look at how this is done correctly . 目前,您要转为生产,请看一下如何正确完成

Hope that helped and happy coding! 希望对您有所帮助,并祝您编程愉快!

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

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