简体   繁体   中英

django cannot read css file in static folder

I am trying to make a base.html template and inserting a css file in the header. in the page it includes all the styling by it does not do any styling when the link the other page is pressed.

I have two files extending base.html one color_choose.html the other statistics.html which have the exact same lines for linking files. color_choose.html works and it is the first page that opens when navigated and the other is statistics.html

here is the base.html:

    <!DOCTYPE html>
    <html lang="eng">
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
        <title>ColorStore</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
        {% block styles %} {%endblock%}
      </head>
      <body>
         <div id="ColorFavor">
           <div style="float: right;">
             <h2 id="title" class="page-name">Color Picker</h2>
           </div>
         </div>
         {% block navigation %}
         {% endblock %}

         {% block display %}
         {% endblock %}

         {% block modal %}
         {% endblock %}

         {% block scripts %}
         {% endblock %}
       </body>
    </html>

here is the urls.py in the app file:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.ColorPageView.as_view(), name='color'),
    path('statistics/',views.StatsPageView.as_view(), name='statistics'),

this is the file css is applied and is also the same text in the other file:

{% extends 'base.html' %}

{% block styles %}
<link rel="stylesheet" href="static/styles/main.css" type="text/css">
{% endblock %}

And this is the part in the settings.py:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

If I am missing anything I will edit this post as soon as possible, just leave a comment for it.

Your template should have {% load static %} and you should refer to the stylesheet either as /static/styles/main.css or (preferably) you should use the macro "{% static styles/main.css %}"

See the django doc here .

您在“ static / ...”之前缺少斜线“ /”

<link rel="stylesheet" href="/static/styles/main.css" type="text/css">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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