简体   繁体   English

Django静态设置

[英]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? 我应该在哪里放置媒体和静态文件夹?

  1. Put your static files into <app>/static or add an absolute path to STATICFILES_DIRS 将您的静态文件放入<app>/static或向STATICFILES_DIRS添加绝对路径
  2. Configure your web server (you should not serve static files with Django) to serve files in STATIC_ROOT 配置Web服务器(不应使用Django提供静态文件)以提供STATIC_ROOT文件
  3. Point STATIC_URL to the base URL the web server serves STATIC_URL指向网络服务器提供的基本URL
  4. Run ./manage.py collectstatic 运行./manage.py collectstatic
  5. Be sure to use RequestContext in your render calls and {{ STATIC_URL }} to prefix paths 一定要使用RequestContext在渲染调用和{{ STATIC_URL }}前缀路径
  6. Coffee and pat yourself on the back 咖啡,轻拍背部

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项目应用程序之外

  • Define your STATICFILES_DIRS, STATIC, and MEDIA path in the settings. 在设置中定义您的STATICFILES_DIRS,STATIC和MEDIA路径。
  • Create staticfiles folder beside your templates folder, urls.py, settings.py, etc... 在您的模板文件夹,urls.py,settings.py等旁边创建staticfiles文件夹...
  • You don't have to create media folder because it will automatically created when you upload images. 您无需创建媒体文件夹,因为它会在您上传图片时自动创建。
  • 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.

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