[英]Django Static Setup
I've looked through countless answers and questions trying to find a single definitive guide or way to do this, but it seems that everyone has a different way. 我遍历了无数答案和问题,试图找到一个确定的指南或方法,但是似乎每个人都有不同的方法。 Can someone please just explain to me how to serve static files in templates?
有人可以向我解释如何在模板中提供静态文件吗?
Assuming I've just created a brand new project with Django 1.4, what all do I need to do to be able to render images? 假设我刚刚使用Django 1.4创建了一个全新的项目,那么我需要做些什么才能渲染图像? Where should I put the media and static folders?
我应该在哪里放置媒体和静态文件夹?
<app>/static
or add an absolute path to STATICFILES_DIRS
<app>/static
或向STATICFILES_DIRS
添加绝对路径 STATIC_ROOT
STATIC_ROOT
文件 STATIC_URL
to the base URL the web server serves STATIC_URL
指向网络服务器提供的基本URL ./manage.py collectstatic
./manage.py collectstatic
RequestContext
in your render calls and {{ STATIC_URL }}
to prefix paths RequestContext
在渲染调用和{{ STATIC_URL }}
前缀路径 A little bit more about running a web server in front of Django. 有关在Django之前运行Web服务器的更多信息。 Django is practically an application server.
Django实际上是一个应用程序服务器。 It has not been designed to be any good in serving static files.
在提供静态文件方面,它的设计还没有任何好处。 That is why it actively refuses to do that when
DEBUG=False
. 因此,当
DEBUG=False
时,它主动拒绝这样做。 Also, the Django development server should not be used for production. 另外,不应将Django开发服务器用于生产。 This means that there should be something in front of Django at all times.
这意味着Django始终都应该有东西。 It may be a WSGI server such as gunicorn or a 'real' web server such as nginx or Apache.
它可能是WSGI服务器(例如gunicorn)或“真实”网络服务器(例如nginx或Apache)。
If you are running a reverse proxy (such as nginx or Apache) you can bind /static
to a path in the filesystem and the rest of the traffic to pass through to Django. 如果您正在运行反向代理(例如nginx或Apache),则可以将
/static
绑定到文件系统中的路径,并将其余流量传递给Django。 That means your STATIC_URL
can be a relative path. 这意味着您的
STATIC_URL
可以是相对路径。 Otherwise you will need to use an absolute URL. 否则,您将需要使用绝对URL。
The created project should have a static folder. 创建的项目应具有一个静态文件夹。 Put all resources (images, ...) in there.
将所有资源(图像,...)放在其中。 Then, in your HTML template, you can reference STATIC_ROOT and add the resource path (relative to the static folder)
然后,在HTML模板中,您可以引用STATIC_ROOT并添加资源路径(相对于静态文件夹)
Here is the official documentation: 这是官方文档:
How to manage static files: https://docs.djangoproject.com/en/1.4/howto/static-files/ 如何管理静态文件: https : //docs.djangoproject.com/en/1.4/howto/static-files/
Static Files in general: https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/ 一般的静态文件: https : //docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/
If you are planning to to deploy in a production type setting then you should read the section here: https://docs.djangoproject.com/en/1.4/howto/static-files/#staticfiles-production 如果您打算在生产类型设置中进行部署,则应在此处阅读以下部分: https : //docs.djangoproject.com/en/1.4/howto/static-files/#staticfiles-production
Generally you put our static and media folders outside of the django project app 通常,您将我们的静态和媒体文件夹放在django项目应用程序之外
In your urlconf put this one: 在您的urlconf中放入以下内容:
from django.conf.urls import patterns, include, url from django.contrib import admin from django.conf import settings from django.contrib.staticfiles.urls import staticfiles_urlpatterns from django.conf.urls.static import static admin.autodiscover() urlpatterns = patterns('', ............. ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) urlpatterns += staticfiles_urlpatterns()
In templates: 在模板中:
{% load static %} <img src="{% static 'images.png' %}"> <img src="{{MEDIA_URL}}images.png">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.