Django 1.4 static file issues and not showing up in other urls of my project

Here are my settings:

STATIC_ROOT = "/home/tony/Documents/mysite/mysite/"

STATIC_URL = '/static/'

STATICFILES_DIRS = (
"/home/tony/Documents/mysite/mysite/static/",
)

      

And here's where I'm referencing my stylesheet (it gives me an error):

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}/css/reset.css" />
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}/css/style.css" />

      

And the error in the log:

[06/Apr/2012 13:36:09] "GET /css/reset.css HTTP/1.1" 404 2193
[06/Apr/2012 13:36:09] "GET /css/style.css HTTP/1.1" 404 2193

      

As I thought I fixed it:

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}static/css/reset.css" />
<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}static/css/style.css" />    

      

And it worked until I made another view:

from django.shortcuts import render_to_response
from django.template import RequestContext

def test(request):
    return render_to_response('test.html')

      

And in the template I added {% extends 'base / base.html'%} and what do I get in the logs? this is:

[06/Apr/2012 13:46:55] "GET /test/ HTTP/1.1" 200 964
[06/Apr/2012 13:46:55] "GET /test/static/css/style.css HTTP/1.1" 404 2229
[06/Apr/2012 13:46:55] "GET /test/static/css/reset.css HTTP/1.1" 404 2229

      

Pay attention to / test /? It doesn't load css.
Any idea why? (I never had this problem with django1.3)

Thank you in advance:)

+3


source to share


2 answers


You are not using RequestContext

to render your template, so no context processors are started, so STATIC_URL

empty.



+2


source


First of all, the following doesn't fix your problem because it STATIC_URL

should contain /static/

, you don't want to write it anywhere, anywhere:

"{{ STATIC_URL }}static/css/reset.css"

      

Now, if you have {{ STATIC_URL }}

a template in your template but /static/

doesn't show up in templates, I think you are missing a template context processor for STATIC_URL

.

Add 'django.core.context_processors.static'

to setting TEMPLATE_CONTEXT_PROCESSORS

. For example, for example:

TEMPLATE_CONTEXT_PROCESSORS = (
   'django.core.context_processors.static',
)

      

If it's TEMPLATE_CONTEXT_PROCESSORS

already in your settings file, you must add it to what already exists.



Also note that it STATIC_URL

ends with a forward slash, so you need to remove the first forward slash after {{ STATIC_URL }}

here:

<link rel="stylesheet" type="text/css" href="{{ STATIC_URL }}/css/reset.css" />

      


Finally, yours is STATIC_ROOT

most likely not what you intended. It should be:

"/home/tony/Documents/mysite/mysite/static/"

      

This is where the static files will be placed when the command is run collectstatic

. See Managing static files in the Django documentation.

+4


source







All Articles