简体   繁体   English

如何在Django项目中构造模板和静态文件?

[英]How do I structure templates and static files in a django project?

I'm pretty confused at how to structure my project such that relative paths in html files work. 我对如何构建项目以使html文件中的相对路径感到困惑。 For example, let's say I have index.html , and in it, I have an image. 例如,假设我有index.html ,并且其中有一个图像。 I have an images folder called img and in it I have cat.png . 我有一个名为img的图像文件夹,其中有cat.png

So, inside my index.html, I have <img src='img/cat.png'> . 因此,在我的index.html中,我有<img src='img/cat.png'> Now, all of these files are in my project's static folder, with STATIC_URL='/static/' , so they get served statically, and it works great: since index.html is in /static/ , then img/cat.png is found with no problem. 现在,所有这些文件都位于我项目的static文件夹中,并且带有STATIC_URL='/static/' ,因此它们是静态提供的,而且效果很好:由于index.html位于/static/ ,因此img/cat.png是发现没有问题。

But here's the problem: 但这是问题所在:

Let's say I want to make index.html a template. 假设我想将index.html用作模板。 So I define a route for it, say /appname/index.html in urls.py , and I make a view for that route in views.py which renders that template with whatever data I need to get into it. 因此,我为此定义了一条路由,在urls.py/appname/index.html ,然后在views.py中为该路由创建一个视图,该视图使用需要进入的任何数据渲染该模板。 Great, now I serve that rendered template, and it is at /appname/index.html . 太好了,现在我为该渲染模板提供服务,它位于/appname/index.html But this means that it will look for all those static files in that path, so for example, it will look for the cat image at /appname/img/cat.png instead of /static/img/cat.png . 但这意味着它将在该路径中查找所有这些静态文件,例如,它将在/appname/img/cat.png而不是/static/img/cat.png处查找cat图像。

Of course I could go through my index.html and change every single relative path to an absolute path, but that isn't great, because then I have to worry more about what will happen if I move files around. 当然,我可以遍历index.html并将每个相对路径更改为绝对路径,但这并不好,因为那样的话我就不得不担心如果移动文件会发生什么情况。 In my particular case, I found that some javascript I'm using loads some CSS dynamically, and I don't want to go in and edit the javascript to all use absolute paths as well. 在我的特定情况下,我发现我正在使用的某些javascript动态加载一些CSS,并且我不想进入并编辑javascript也都使用绝对路径。

So what's up? 那么这是什么一回事? Is Django just bad at serving static content, and I should be using nginx or apache for that instead? Django是否只擅长提供静态内容,而我应该为此使用nginx或apache? I know Django is supposed to be able to function as a full production server, so I don't know what's up here. 我知道Django应该能够用作完整的生产服务器,所以我不知道这里发生了什么。 Any thoughts? 有什么想法吗?

You should be using a combination of your templating language, middleware, and some configuration. 您应该结合使用模板语言,中间件和一些配置。 Directions for how to do this can be found here - https://docs.djangoproject.com/en/1.8/howto/static-files/ 有关如何执行此操作的说明,请参见此处-https://docs.djangoproject.com/en/1.8/howto/static-files/

  1. Make sure that django.contrib.staticfiles is included in your INSTALLED_APPS. 确保django.contrib.staticfiles包含在INSTALLED_APPS中。
  2. In your settings file, define STATIC_URL , for example: 在您的设置文件中,定义STATIC_URL ,例如:

    STATIC_URL = '/static/'

  3. In your templates, either hardcode the url like /static/my_app/myexample.jpg or, preferably, use the static template tag to build the URL for the given relative path by using the configured STATICFILES_STORAGE storage (this makes it much easier when you want to switch to a content delivery network (CDN) for serving static files). 在您的模板中,对网址进行硬编码(例如/static/my_app/myexample.jpg或者最好使用静态模板标签通过配置的STATICFILES_STORAGE存储来为给定的相对路径构建URL(这使您在需要时更加容易切换到内容分发网络(CDN)来提供静态文件)。

    {% load staticfiles %}

    img src="{% static "my_app/myexample.jpg" %}" alt="My image"

  4. Store your static files in a folder called static in your app. 将您的静态文件存储在应用程序中名为static的文件夹中。 For example my_app/static/my_app/myimage.jpg. 例如my_app/static/my_app/myimage.jpg.

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

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