如何在 Django 项目/应用程序中组织和加载 CSS? [英] How to organize and load CSS within Django project/app?

查看:16
本文介绍了如何在 Django 项目/应用程序中组织和加载 CSS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Django 的新手.我阅读了文档,我对媒体根文件和静态根文件以及我应该将我的 css 文件和我的 javascript 文件放在哪里感到困惑.

I'm new to Django. I read the Documentation and I am literally confused with media root files and static root files and where I should be placing my css files along with my javascript files.

你们能指出我正确的方向吗?项目设置:

Can you guys point me in the correct direction? Project setup:

Project
     -myapp
         -model,view,and test
     -template
         - base.html
         - index.html
         - view1
         - view2
         - media
             - css
                 - style.css
                 - ie.css
             - js
             - img

     -setting.py
     -url.py

我的 static_url、static_root、media_url、media_root、STATICFILES_DIRS 和 STATICFILES_FINDERS 应该是什么样的?

What should my static_url, static_root, media_url , media_root, STATICFILES_DIRS and STATICFILES_FINDERS look like?

我目前如何通过 base.html 加载我的 css 文件,如下所示:

How I currently load my css file is through the base.html with the following:

{% load staticfiles %}
<link rel="stylesheet" type="text/css" href="{% static user_stylesheet %}"type="text/css" media="screen"/>

那么管理静态文件的最佳和最有效/正确的方法是什么?谢谢大家的帮助.感谢您的时间和努力.

So what is the best and most efficient/correct way of managing my static files? Thank you everybody for your help. I appreciate your time and effort.

推荐答案

静态文件是一个简单的过程:

Static files are a simple process:

  • Django 将在 INSTALLED_APPS 中的任何应用程序中搜索名为 static/ 的目录以查找静态文件.如果您有未绑定到任何应用程序的文件,您可以将它们放在单独的目录中.这个目录应该被添加到 STATICFILES_DIRS(一个元组)中,以便 django 知道它.在你的情况下,你应该有 STATICFILES_DIRS = ('/home/user/Project/template/media',)

  • Django will search for a directory named static/ inside any app that is in INSTALLED_APPS for static files. If you have files that are not tied to any app, you can put them in a separate directory. This directory should be added to STATICFILES_DIRS (a tuple) so django is aware of it. In your case you should have STATICFILES_DIRS = ('/home/user/Project/template/media',)

django.contrib.staticfiles 添加到您的 INSTALLED_APPS

现在,如果您正在使用 runserver 进行测试,请打开您的 urls.py 并添加 from django.contrib.staticfiles.urls import staticfiles_urlpatterns 到顶部,在定义所有 URL 后,在文件末尾添加 urlpatterns += staticfiles_urlpatterns()

Now, if you are testing this with runserver, open your urls.py and add from django.contrib.staticfiles.urls import staticfiles_urlpatterns to the top, and after all your URLs are defined, at the end of the file, add urlpatterns += staticfiles_urlpatterns()

您已经有了访问问题中静态文件的正确方法.另一种方法是使用 {{ STATIC_URL }},并确保您使用 RequestContext 发送响应.如果您使用基于类的视图,则无需更改任何内容.对于其他普通视图方法,只需使用 render 快捷方式.

You already have the correct method of accessing static files in your question. The other way is to use {{ STATIC_URL }}, and make sure you are sending your responses with RequestContext. If you are using class based views, you don't have to change anything. For your other normal view methods, just use the render shortcut.

STATIC_URLSTATIC_ROOT 在您部署时会派上用场.

STATIC_URL and STATIC_ROOT come in handy when you are deploying.

  • STATIC_URL 是您的 Web 服务器配置的 URL 前缀,用于指向系统中包含静态文件的位置.这是一个 url 组件必须以斜线结尾.例如 /static/

  • STATIC_URL is the URL prefix that your web server has been configured with to point to the location in the system that has your static files. This is a url component and must end in a slash. For example /static/

STATIC_ROOT 这是系统目录的路径.当您准备好部署时,运行 collectstatic 命令,django 将找到您所有的静态文件并将它们转储到 STATIC_ROOT 指向的目录中.然后你可以把这个目录放在你的 DOCUMENT_ROOT 文件夹中,你的 web 服务器是用它配置的.然后,将 /static/ URL 指向此文件夹.示例是 STATIC_ROOT =/home/user/project/www/

STATIC_ROOT this is a path to a directory on your system. When you are ready to deploy, run the collectstatic command and django will find all your static files and dump them in the directory pointed to by STATIC_ROOT. Then you can take this directory, put it in your DOCUMENT_ROOT folder that your web server is configured with. Then, point the /static/ URL to this folder. Example is STATIC_ROOT = /home/user/project/www/

如果 Apache 配置了 /var/www/DOCUMENT_ROOT,并且您的 STATIC_URL 设置为 /static/;运行 collectstatic 后,将文件夹 /home/user/project/www/ 的内容移动到 /var/www/static/.

If Apache is configured with a DOCUMENT_ROOT of /var/www/, and your STATIC_URL setting is /static/; after you run collectstatic, move the contents of the folder /home/user/project/www/ to /var/www/static/.

  • STATICFILES_FINDERS.此设置列出了 django 在您运行 collectstatic 时可以用来搜索静态文件的方法,您通常根本不需要修改它.
  • STATICFILES_FINDERS. This setting lists methods that django can use to search for your static files when you run collectstatic, and you normally don't modify this at all.
  • MEDIA_ROOTMEDIA_URL.这些设置控制文件系统位置,以及使用 django 上传的任何文件的 URL 前缀;这些不能与STATICFILES_DIRS相同.您需要手动处理此问题,因为 collectstatic 不会触及这些位置.如果您正在开发中测试文件上传,您可以使用 django.views.static.serve() 提供来自 MEDIA* 的文件.
  • MEDIA_ROOT, MEDIA_URL. These settings control the file system location, and the URL prefix of any files that are uploaded using django; these cannot be the same as STATICFILES_DIRS. You need to handle this manually as collectstatic will not touch these locations. If you are testing file uploads in development, you can use django.views.static.serve() to serve files from MEDIA*.

这篇关于如何在 Django 项目/应用程序中组织和加载 CSS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆