سؤال

I have a problem with the landing styles. I do not know what happened, because the earlier work.

Files using the site, I have in the "static". Files which are used, for example, django admin panel - in the "staticfiles"

My settings.py file, on the part of the static files:

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

TEMPLATE_DIRS = (
     join (base_dir, 'templates'),
)

STATICFILES_DIRS = (
     join (base_dir, 'static')
)

STATIC_URL = '/static/'

STATIC_ROOT = (
     join (base_dir, 'staticfiles')
)

HTML code, base.html. This is my main page:

<!DOCTYPE html>
<html>
<head>
    <title>Test site</title>
    <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}/css/main.css">
</head>

<body>
    <div class="container">
    {% block nav %}
      <div class="header">
        <ul class="nav nav-pills pull-right">
          <li {% if request.path == '/' %} class="active" {% endif %}>
              <a href="/">Home</a>
          </li>
            {% if user.is_authenticated %}
          <li{% if request.path == '/elist/'%} class="active" {%endif%}>
              <a href="{% url 'emaillist' %}">E-mail list</a>
          </li>
            {% endif %}
          <li{% if request.path == '/contact/' %} class="active" {% endif %}>
              <a href="{% url 'contact1' %}">Contact</a>
          </li>
        </ul>
        <h3 class="text-muted"><a href="/">Name site</a></h3>
      </div>
    {% endblock %}

    {% block header %}
      <div class="jumbotron">
        <h1>Soon we go!</h1>
        <p class="lead">Landing page.</p>
      </div>
    {% endblock %}

    {% block content %}{% endblock %}

    {% block footer %}{% endblock %}

    </div>
  </body>
</html>
هل كانت مفيدة؟

المحلول

This doesn't look good:

STATIC_ROOT = (
     join (base_dir, 'staticfiles')
)

This variable should be a string, not a tuple, like this:

STATIC_ROOT = join(base_dir, 'staticfiles')

But I think you probably want to do this instead:

STATICFILES_DIRS = (
    join(base_dir, 'staticfiles')
)

Make sure that base_dir is an absolute path, and that staticfiles/css/main.css really exists relative to it.

And since you're in Django 1.6, it's better to use {% static 'css/main.css' %} instead of {{ STATIC_URL }}/css/main.css. For that you'll have to do {% load staticfiles %} earlier in the file, ideally near the top.

The documentation explains very nicely the difference between these variables. In a nutshell:

  • During development, if you have django.contrib.staticfiles in INSTALLED_APPS, then Django will discover static files in the static directories in your apps and serve them from there.
  • You can use STATICFILES_DIRS to specify additional directories where Django should look for static files.
  • STATIC_ROOT is something use in deployment, when DEBUG=False. This should be an absolute path in the filesystem, a path used by your web server like Apache (NOT Django's built-in development server). When you run python manage.py collectstatic, Django will gather all static files in your project and copy them to this common location.
  • STATIC_URL is the base URL of static files. Typically /static/. In deployment, this URL will not be served by Django, but directly by the web server, from the location of STATIC_ROOT.

نصائح أخرى

STATIC_ROOT = os.path.join(PROJECT_DIR,'static')

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR,'staticfiles'), # if you name your static files folder as "staticfiles"
)

Now in templates include staticfiles as

<link rel="stylesheet" type="text/css" href="{% static 'css/main.min.css' %}" />        
<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top