从该主题上类似标题的数量来看,这似乎是一个令人困惑的来源,但是尝试了我在 django 开发服务器上在静态文件上能找到的所有内容,我几乎放弃了希望!
所以我的静态文件是从C:/Users/Dan/seminarWebsite/static/提供的,其中我有图像、css等的子文件夹。
设置:
STATIC_ROOT = 'C:/Users/Dan/seminarWebsite/static/'
STATIC_URL = '/static/'
静态文件应用程序也处于活动状态。
URLS:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
模板:
"{{ STATIC_URL }}images/vision.jpeg"
但是,仅出现一个损坏的链接,地址为:http://127.0.0.1:8000/homepage/images/vision.jpeg< /code> 我认为它不应该位于该地址(主页是调用静态图像文件的页面的 URL 名称)。
This seems to be a source of much confusion judging by the amount of similar titles on the subject, however trying everything I could find on static files with the django development server I've almost given up hope!
So my static files are served from C:/Users/Dan/seminarWebsite/static/, in which I have sub folders for images, css etc.
SETTINGS:
STATIC_ROOT = 'C:/Users/Dan/seminarWebsite/static/'
STATIC_URL = '/static/'
The static files app is also active.
URLS:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += staticfiles_urlpatterns()
TEMPLATE:
"{{ STATIC_URL }}images/vision.jpeg"
However only a broken link appears and at this address: http://127.0.0.1:8000/homepage/images/vision.jpeg
and I don't think it should be at that address (homepage is the url name of the page the static image file is being called to).
发布评论
评论(2)
根据您迄今为止发布的内容,您似乎正在遵循
django.contrib.staticfiles
。我同意文档可能很难理解,尤其是对于 django 新手来说。我相信这种混乱源于
django.contrib.staticfiles
有两种操作模式:STATIC_URL
STATIC_ROOT
定义),以便可以使用适合静态文件的网络服务器托管静态文件。此排序规则是使用 python ./manage.pycollectstatic 完成的。以下是有关如何启动和运行的快速摘要。我还没有机会尝试,所以可能会有错误。希望这将帮助您入门,并至少帮助您理解文档。如有疑问,请参阅文档。
在开发服务器上托管静态文件
确保
INSTALLED_APPS
中有'django.contrib.staticfiles'
指定
STATIC_URL
。这将是托管静态文件的路径。<前><代码> STATIC_URL = '/静态/'
确保您的文件位于正确的目录中。默认情况下,
staticfiles
将在每个已安装应用程序的static/
目录以及STATICFILES_DIRS
。 (此行为取决于STATICFILES_FINDERS< 中列出的后端/代码>
)。
在您的情况下,您可能希望在 STATICFILES_DIRS 中指定您的目录:
<前><代码> STATICFILES_DIRS = (
'C:/Users/Dan/seminarWebsite/static/',
)
通过将以下内容添加到
url 的末尾,使视图可访问。 py
:确保
DEBUG = True
中>settings.py.就是这样。
如果您运行开发服务器 (
./manage.py runserver
),您应该能够通过http://localhost:8000/static/images/vision.jpeg< 访问您的文件/code>(服务于
C:/Users/Dan/seminarWebsite/static/images/vision/jpeg
)。使用模板标签链接到模板中的静态文件
在 Django 1.10 之前,您可以使用以下内容:
从 Django 1.10 及更高版本,您应该使用:
使用 STATIC_URL 链接到模板中的静态文件
有两种方法可以为您的静态文件获取正确的链接文件 - 使用 静态文件模板标记,如上所述,这是首选方法,并使您的模板可以访问
STATIC_URL
。既然您已经尝试过后者,那么这里是如何做到这一点的。如果使用 Django 版本低于 1.10,请确保您有
'django.core.context_processors.static'
或'django.template.context_processors.static'
如果使用版本从TEMPLATE_CONTEXT_PROCESSORS
1.10 开始。如果您还没有重新定义TEMPLATE_CONTEXT_PROCESSORS
,那么自默认情况下应该存在。确保使用 RequestContext 渲染模板时。示例:
您现在应该能够在
my_template.html
中使用以下内容:在生产服务器上托管静态文件。
如果您需要提供的所有静态文件都存储在该目录中 (
C:/Users/Dan/seminarWebsite/static
),那么您就差不多完成了。只需将您的网络服务器配置为在/static/
(或您设置的STATIC_URL
的任何内容)上托管该目录,就可以开始了。如果您的文件分散在不同的目录和/或应用程序特定的静态文件中,那么您需要整理它们。
设置
STATIC_ROOT
< /a> 到要存储整理文件的目录。运行
./manage。 pycollectstatic
进行排序。配置您的网络服务器以在
/static/
(或您设置STATIC_URL
的任何内容)上托管该目录 (STATIC_ROOT
)。Based on what you've posted so far, it looks like you're following the docs for
django.contrib.staticfiles
. I agree that the docs can be difficult to follow especially if one is new to django.I believe the confusion stems from the fact that
django.contrib.staticfiles
has two modes of operation:STATIC_URL
STATIC_ROOT
) so that the static files can be hosted using a webserver suited for static files. This collation is done usingpython ./manage.py collectstatic
.Here's a quick summary of how to get up and running. I haven't had a chance to try it out so there may be mistakes. Hopefully this will help you get started and at the very least help you understand the docs. When in doubt, do refer to the docs.
Hosting static files on the development server
Make sure you have
'django.contrib.staticfiles'
inINSTALLED_APPS
Specify
STATIC_URL
. This will be the path where your static files will be hosted on.Make sure your files are in the correct directories. By default,
staticfiles
will look for files within thestatic/
directory of each installed app, as well as in directories defined inSTATICFILES_DIRS
. (This behaviour depends on backends listed inSTATICFILES_FINDERS
).In your case, you'd probably want to specify your directory in
STATICFILES_DIRS
:Make the view accessible by adding the following to the end of
urls.py
:Make sure you have
DEBUG = True
insettings.py
.That's it.
If you run your dev server (
./manage.py runserver
), you should be able to access your file viahttp://localhost:8000/static/images/vision.jpeg
(which servesC:/Users/Dan/seminarWebsite/static/images/vision/jpeg
).Linking to static files in your template using template tags
Before Django 1.10 you can use the following:
From Django 1.10 and later you should use:
Linking to static files in your templates by using STATIC_URL
There are two ways to get a correct link for your static files - using the staticfiles template tag as described above which is the preferred method, and making
STATIC_URL
accessible to your templates. Since you've attempted the latter, here's how to do that.Make sure you have
'django.core.context_processors.static'
if using Django version less than 1.10 or'django.template.context_processors.static'
if using versions from 1.10 onwards inTEMPLATE_CONTEXT_PROCESSORS
. If you haven't redefinedTEMPLATE_CONTEXT_PROCESSORS
then there is nothing to do since it should be there by default.Make sure you use RequestContext when rendering your template. Example:
You should now be able to use the following in your
my_template.html
:Hosting static files on production server.
If all the static files you need to serve are store in that one directory (
C:/Users/Dan/seminarWebsite/static
), then you're almost there. Simple configure your webserver to host that directory on/static/
(or whatever you setSTATIC_URL
to) and you're good to go.If you have files scattered in different directories and/or app specific static files, then you'll need to collate them.
Set
STATIC_ROOT
to the directory where you want to store the collated files.Run
./manage.py collectstatic
to do the collation.Configure your webserver to host the that directory (
STATIC_ROOT
) on/static/
(or whatever you setSTATIC_URL
to).我想分享我对当地发展的看法。它与 Shawn Chin 的答案类似,但不需要
DEBUG=True
和特殊的 urlpatterns,我认为这更通用和干净。第 1、2、3 点与答案中的相同,但以下内容有所不同:
与 Django 4.2 相关
I'd like to share my reciept for a local development. It's similar to Shawn Chin's answer, but not required
DEBUG=True
and special urlpatterns, which is more universal and clean as I think.Points 1, 2, 3 are the same as in the answer, but the following are different:
Relevant for Django 4.2