所以,再次,我如何告诉Django从我的STATIC_ROOT位置加载静态文件...而不是从我的项目和应用程序中的静态目录加载?或者,我是否误解了Django不应该从我的STATIC_ROOT位置本地加载文件? *编辑(添加HTML图像) 解决方案我认为您正在混淆一些事情.让我澄清一下. > static: 根据文档,它提供了用于提供静态文件的URL模式.如果您转到 实施 ,那么您将看到: 返回[re_path(r'^%s(?P< path>.*)$'%re.escape(prefix.lstrip('/')),view,kwargs = kwargs),] 它的作用是,它从前缀的左边删除正斜杠(即将/static/转换为 static/),然后有一个view(是 serve )由谁负责提取文件. serve: 此功能执行投放文件.它将从文档根目录提供文件. runserver: runserver 命令运行django开发服务器.如果在 INSTALLED_APPS 中安装了 django.contrib.staticfiles ,则它将自动提供静态文件.如果您不希望提供静态服务,请使用 runserver --nostatic . collectstatic 或 STATIC_ROOT 与该命令无关. collectstatic :此命令从不同的 STATIC_DIRS 收集所有静态文件,并将它们放置在 STATIC_ROOT 定义的文件夹中.当您使用反向代理服务器(如NGINX或Apache等)时, collectstatic 在生产部署中非常有用.NGINX/Apache/Varnish使用该文件夹(collectstatic存储静态文件的文件夹)作为根目录并提供静态服务从中.不建议在生产中使用 runserver .您可以使用gunicorn/uwsgi服务django.但是gunicorn/uwsgi不提供静态文件,因此使用反向代理服务器提供静态文件. 最终: 要回答您的问题: 否,除非您没有在 INSTALLED_APPS 中添加 django.contrib.staticfiles ,否则不必将其放入代码中. 否 您不需要.STATIC_ROOT用于不同目的 不是.但是,要提供MEDIA文件,您可以添加以下模式: 如果设置.调试:urlpatterns + = [re_path(r'^ media/(?P&path;.*)$',服务,{'document_root':settings.MEDIA_ROOT,}),] 在生产中,媒体文件也应由反向代理服务器提供服务.I am using Django 2.2. From the Django Managing static files documentation: If you use django.contrib.staticfiles as explained above, runserver will do this automatically when DEBUG is set to True. If you don’t have django.contrib.staticfiles in INSTALLED_APPS, you can still manually serve static files using the django.views.static.serve() view. This is not suitable for production use! For some common deployment strategies, see Deploying static files. For example, if your STATIC_URL is defined as /static/, you can do this by adding the following snippet to your urls.py:from django.conf import settingsfrom django.conf.urls.static import staticurlpatterns = [ # ... the rest of your URLconf goes here ...] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) Note This helper function works only in debug mode and only if the given prefix is local (e.g. /static/) and not a URL (e.g. http://static.example.com/). Also this helper function only serves the actual STATIC_ROOT folder; it doesn’t perform static files discovery like django.contrib.staticfiles.My Interpretationstatic is a helper function that serves files from the STATIC_ROOT during development (is this True?)static only works when debug = Truestatic only works with a local prefix like STATIC_URL = '/static/'When DEBUG is set to True and I use and setup the staticfiles app as explained in the documentation, if I do python manage.py runserver to start the local server, the serving of static files will be handled automatically (true??)My QuestionsWhat EXACTLY does adding static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) to your project's urls.py DO?Is it true that static serves static files locally from the STATIC_ROOT directory? To test this theory, after running collectstatic, I then deleted the static directories to see if the static files still load fine (from the STATIC_ROOT) and they DON'T! Why?How can I verify that Django is loading the static files from my STATIC_ROOT location... and not the static directories in my project and apps??Why is adding static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) to urlpatterns necessary if Django serves static files automatically (mentioned in documentation)?Examplesettings.pyDEBUG = True...INSTALLED_APPS = [ 'django.contrib.admin', ... 'django.contrib.staticfiles', 'puppies.apps.PuppiesConfig']...STATIC_URL = '/static/'STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"),]STATIC_ROOT = 'c:/lkdsjfkljsd_cdn'In all my templates, I'm using {% load static %}.Then I do: python manage.py collectstaticAt this point, it doesn't seem to matter if I have the below in my urls.py or not - my static files still load BUT I don't know if they're coming from my project's static directories or my STATIC_ROOT (c:/lkdsjfkljsd_cdn):if settings.DEBUG is True: urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)Lastly, if I delete those static directories in my project, all css, js and images don't work which leads me to believe that my Django project is loading static files from my project's static directories, NOT the STATIC_ROOT.So, again, how can I tell Django to load the static files from my STATIC_ROOT location... and not the static directories in my project and apps?? OR, do I misunderstand that Django isn't supposed to load files from my STATIC_ROOT location locally?*Edit (adding HTML image) 解决方案 I think you are mixing up few things. Let me clarify.static:As per documentation, it provides URL pattern for serving static file. If you go to the implementation, then you will see:return [ re_path(r'^%s(?P<path>.*)$' % re.escape(prefix.lstrip('/')), view, kwargs=kwargs),]What it does is, it removes forward slash from left of the prefix(ie converts /static/ to static/), and then there is a view(which is serve) who does the pulling files.serve:This function does the serving files. It will serve files from a document root.runserver:runserver command runs the django development server. If you have django.contrib.staticfiles installed in INSTALLED_APPS, then it will automatically serve static files. If you don't want to serve static, then use runserver --nostatic. collectstatic or STATIC_ROOT has no relation to this command.collectstatic:This command collects all static files from different STATIC_DIRS, and put them in folder which is defined by STATIC_ROOT. collectstatic is very useful in production deployment, when you are using a reverse proxy server like NGINX or Apache etc. NGINX/Apache/Varnish uses that folder (where collectstatic stored the static files) as root and serve static from it. It is not recommended to use runserver in production. You can use gunicorn/uwsgi to serve django. But gunicorn/uwsgi does not serve static files, hence using reverse proxy server to serve static files.finally:To answer your questions:No you don't have to put that in your code, unless you are not adding django.contrib.staticfiles in your INSTALLED_APPS.NoYou don't need to. STATIC_ROOT is used for different purposeIt is not. But for serving MEDIA files, you can add the following pattern:if settings.DEBUG: urlpatterns += [ re_path(r'^media/(?P<path>.*)$', serve, { 'document_root': settings.MEDIA_ROOT, }), ]In production, media files should be served by reverse proxy server as well. 这篇关于Django静态(settings.STATIC_URL,document_root = settings.STATIC_ROOT)实际上有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-26 03:24