1

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>
Mariusz
  • 177
  • 1
  • 4
  • 11
  • `STATIC_URL = '/ static /'` maybe it's cause of space before and after `static`? – waldek_h Mar 29 '14 at 14:33
  • Why do you have spaces between the slashes and static in `STATIC_URL`? Edit: Bah, humbug, waldek_c beat me to it. – Enrico Mar 29 '14 at 14:34
  • @waldek_c no, this in not a problem. – Mariusz Mar 29 '14 at 14:35
  • ok, second try: `{% load staticfiles %} ` if it's django 1.4+ as in [http://stackoverflow.com/questions/11683748/django-static-url-is-not-working#answer-11688372] – waldek_h Mar 29 '14 at 14:43
  • @waldek_c It is django 1.6.2, but this solution is not working. Mayby it's something with STATIC_URL or STATIC_ROOT? – Mariusz Mar 29 '14 at 14:46
  • `{% load static from staticfiles %}` as in doc https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#template-tags it changed after 1.5 – waldek_h Mar 29 '14 at 14:53
  • @waldek_c This isn't solution. Not working. – Mariusz Mar 29 '14 at 15:01

2 Answers2

2

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.
janos
  • 120,954
  • 29
  • 226
  • 236
0
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>
4dgaurav
  • 11,360
  • 4
  • 32
  • 59
  • add this at the top of the html file {% load static %} if you wish to use static files in and others such tags.. – 4dgaurav Apr 03 '14 at 09:17