如何在 Django 中使用 CSS?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/261223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 20:04:44  来源:igfitidea点击:

How do I use CSS in Django?

cssdjangodjango-forms

提问by gath

I am creating my application using Django, and am wondering how I can make Django use my CSS file? What settings do I need to do to make Django see the css file?

我正在使用 Django 创建我的应用程序,并且想知道如何让 Django 使用我的 CSS 文件?我需要做哪些设置才能让 Django 看到 css 文件?

NB: On a local machine

注意:在本地机器上

采纳答案by Joe

If you're using the development server follow the django project's how-to guide for managing static filesto setup your URL's, then reference you media files in the template -- say, an image inside an image folder from /site_media/images/foo.gif.

如果您正在使用开发服务器,请按照django 项目的操作指南管理静态文件来设置您的 URL,然后在模板中引用您的媒体文件——例如,来自/site_media/images/foo.gif.

回答by Peter Rowell

More generally stated, you're asking how to serve a static file from Django. If you are running under Apache, you should read http://docs.djangoproject.com/en/dev/howto/deployment/modpython/

更笼统地说,您是在询问如何从 Django 提供静态文件。如果你在 Apache 下运行,你应该阅读http://docs.djangoproject.com/en/dev/howto/deployment/modpython/

If you are running the development server (say, on your laptop), read http://docs.djangoproject.com/en/dev/howto/static-files/

如果您正在运行开发服务器(例如,在您的笔记本电脑上),请阅读http://docs.djangoproject.com/en/dev/howto/static-files/

Do note the big, fat disclaimerregarding the Django development server:

请注意关于 Django 开发服务器的大而丰厚的免责声明

  • Using this server is inefficient and insecure.
  • Do not use this in a production setting.
  • Use this only for development.
  • 使用此服务器效率低下且不安全。
  • 不要在生产环境中使用它。
  • 仅将其用于开发。

回答by jacanterbury

This caused me problems too for a while (404 not found errors). The missing bit for me was to edit the STATICFILES_DIRStuple in settings.py to give me this:

这也给我带来了一段时间的问题(404 not found 错误)。我缺少的一点是编辑STATICFILES_DIRSsettings.py 中的元组给我这个:

STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(os.path.dirname(__file__),'media').replace('\','/'),
)

This then picked up my CSS files in a folder called 'media' that was at the top level of my django project.

然后在我的 django 项目顶层名为“media”的文件夹中提取我的 CSS 文件。

I also had:

我也有:

MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/media/'

(make sure you have the leading /above in STATIC_URL)

(请确保已经领先/以上STATIC_URL

Of course, as said above, you need to have the CSS file properly included from your html files. I had:

当然,如上所述,您需要从 html 文件中正确包含 CSS 文件。我有:

<link href="{{ STATIC_URL }}css/ea_base.css" rel="stylesheet" type="text/css" media="screen" />

回答by Oli

What settings do i need to do to make Django see the css file?

我需要做哪些设置才能让 Django 看到 css 文件?

None.

没有任何。

Make sure your template includes the CSS file (as standard HTML does) and put the CSS file on the media server.

确保您的模板包含 CSS 文件(与标准 HTML 一样)并将 CSS 文件放在媒体服务器上。

To clarify: With Django it is highly recommended that you serve all your media (everything that isn't dynamic html) from a different server instance. How you implement that is completely up to you but most people create a subdomain.

澄清一下:对于 Django,强烈建议您从不同的服务器实例提供所有媒体(所有不是动态 html 的内容)。您如何实现完全取决于您,但大多数人会创建一个子域。

回答by James Selvakumar

The official django docs didn't help me. Hope the blog post "Django: How to serve static files" helps some of you.

官方 django 文档没有帮助我。希望博客文章“ Django:如何提供静态文件”对你们中的一些人有所帮助。

回答by Roch

Well the easiest way to use css with django, is to add it to your templates as static-files.

在 django 中使用 css 的最简单方法是将其作为静态文件添加到模板中。

But it's a bit like ajax, I didn't find anything that tells how to include it in a standard way.

但它有点像 ajax,我没有找到任何说明如何以标准方式包含它的内容。

There is a css-compressormodule for django if you want to optimise its size.

如果你想优化它的大小,django有一个css-compressor模块。