Django中style.css文件的位置 [英] style.css file location in django

查看:38
本文介绍了Django中style.css文件的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个style.css文件,该文件在Django项目中的所有应用程序中都通用.它的位置如下:

I have a style.css file which is common across all the apps in my Django project. It is located as follows:

testsite______
              |
              |
           testsite
              |
              |
             news 
              |
              |
            reviews
              |
              |
           templates------>static------>css------> style.css
              |
              |
           manage.py

在项目的settings.py中,我有 STATICFILES_DIRS 如下:

In settings.py of the project, I have STATICFILES_DIRS as follows:

STATIC_URL = '/static/'
STATICFILES_DIRS = (
        "/path/to/my/project/templates/static/",
)

我按如下方式在模板标题中调用css文件:

I call the css files in the header of my template as follows:

<html>
    <head>
        <title>TEst site</title>
        <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}css/style.css"/>
    </head>

但是,css无法加载.它给出404错误,其中服务器在应用程序内查找css文件(新闻,评论).错误如下:

However, the css doesn't load. It gives a 404 error wherein the server looks for the css file inside the app (news, reviews). The error is as follows:

"GET /test-site/css/style.css HTTP/1.1" 404 4147

我是否需要在urls.py中为CSS指定一些内容?我在这里错过了什么?

Do I need to specify something for css in the urls.py? What am I missing out here?

推荐答案

不,您不必在url中指定任何CSS.尝试放入HTML

No, you dont have to specify anything of the css in the urls. Try to put into the HTML

{% load staticfiles %}

并呼叫

<link rel="stylesheet" href="{% static 'css/style.css' %}" />

我那样做,而且效果很好.

I do it like that and it works fine.

这篇关于Django中style.css文件的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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