including css in Django

ぃ、小莉子 提交于 2019-12-20 02:10:28

问题


I'm new to Django, and i'm having hard time including css styles in a template.

I read this and tried to do the same but it's not working for me.

my Template:

{% load static %}<html><head><link href="{% get_static_prefix %}/style.css" rel='stylesheet' type='text/css' /></head><body>

the HTML i get:

<head><link href="C:/Users/Nayish/workspace/am/src/am/static/style.css"rel='stylesheet'type='text/css' /></head>

Note that this is the folder containing my css.

Thanks, Boris.


回答1:


I am guessing you aren't using static css sheets. I always just do:

<html>
<head>
            {%block stylesheet %}
               <style type="text/css" title="currentStyle"> 
                   @import "{{MEDIA_URL}}css/style.css";
               </style>
            {% endblock stylesheet%}
   ....

I then set my Media root, and store the files as

 MEDIA_ROOT=<fullyquallified patyh>/Media/css/<css files>
 MEDIA_URL=http://localhost/mysite/

It should be noted that STATIC_URL defaults to MEDIA_URL if its not defined.




回答2:


Make sure you haven't mixed up the STATIC_ROOT and STATIC_URL settings.

STATIC_ROOT defines where the files are on the storage system (usually your local hard disc for local development), while STATIC_URL defines the URL from where the server serves them. The second one is usually referred to in templates, and it is also the value that the {% get_static_prefix %} template tag returns.



来源:https://stackoverflow.com/questions/6125661/including-css-in-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!