2

I am developing a Django site and after a series of edits I ran my server again when suddenly - no css! The server still displays the html site, of course, but the css all across the site is throwing a 404 error. My static files information in my settings.py wasn't edited at all:

import os
# hack to accommodate Windows
CURRENT_PATH = os.path.abspath(os.path.dirname(__file__).decode('utf-8')).replace('\\', '/')

STATIC_URL = '/static/'

# Additional locations of static files
STATICFILES_DIRS = (
    os.path.join(CURRENT_PATH, 'static'),
)

My base template does a simple call for the file based on the static file directory:

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

And lastly, of course, here is a quick breakdown of my pertinent directory structure, since I suspect the error is some kind of directory issue:

-project-
     settings.py
     -static-
         css.css
     -templates-
         base.html
         index.html

I've only provided the location of index.html, but the templates folder of course is filled with directories for various templates/pages.

I'll keep this question updated with information as I troubleshoot/receive answers.

fildred13
  • 2,280
  • 8
  • 28
  • 52
  • How do your CSS URLs look like? Do you use the dev server or something else? – wRAR Mar 26 '13 at 16:01
  • 1
    have you set the DEBUG variable to False ? – PepperoniPizza Mar 26 '13 at 16:03
  • I just use the shell devserver, so when I call up localhost:8000, which is returning my index, the css url and accompanying error is simply `/css.css/ HTTP/1.1" 404`. The next level down, which gives a little more detail though, is `/category/earrings/css.css HTTP/1.1"`. That second level comes from a "category.html" located in its own directory within the templates directory. – fildred13 Mar 26 '13 at 16:05
  • your CURRENT_PATH code? – catherine Mar 26 '13 at 16:05
  • @PepperoniPizza, my DEBUG had been set to True during all of this, since I am in active development. As a test, I set it to False for just a moment, and suddenly every page of my site was simply returning a 500 error! Something to worry about for the future, I suppose... – fildred13 Mar 26 '13 at 16:07
  • @catherine ah yes, I forgot to include that. I'll add it to the post now, but here it is: `import os # hack to accommodate Windows CURRENT_PATH = os.path.abspath(os.path.dirname(__file__).decode('utf-8')).replace('\\', '/')` – fildred13 Mar 26 '13 at 16:08
  • try: `CURRENT_PATH = os.path.dirname(__file__)` only – catherine Mar 26 '13 at 16:10
  • @catherine I tried it, but the error persists. – fildred13 Mar 26 '13 at 16:14
  • where is your STATIC_ROOT? and Have you define static in your root urls.py? – catherine Mar 26 '13 at 16:20
  • I think they must update their error messages because they lead us in the wrong direction – catherine Mar 26 '13 at 16:32

2 Answers2

2

There are a number of things you need to do in order to get the static urls working on the development platform.

For this i would recommend looking through and reading this link. https://docs.djangoproject.com/en/dev/howto/static-files/

As well as this you also need to return a request context from your view.

Note that within your urls.py file you will also need to add the line.

urlpatterns += staticfiles_urlpatterns()

This line allows the URL's module within a development server to serve static files.

I also notice that you have a variable within your os.path.join code. os.path.join will already be pointing at you development directory if you are using a basic django setup.

You should be able to use:

# Additional locations of static files
STATICFILES_DIRS = (
    os.path.join('static'),
)

Note: Also make sure your views are a returning a request context, as this is required for the STATIC_URL variable to be populated. Likewise, ensure you have all the TEMPLATE_CONTEXT_PROCESSORS added into the settings.py file

Matt Seymour
  • 8,880
  • 7
  • 60
  • 101
  • I'll look through the link right now, along with your other information in just a moment, but I should point out that the static files WERE working just a few hours ago, but something strange happened during my editing of what I THOUGHT was completely unrelated material from the static file path. Also, my `urlpatterns` serves using this code: `url(r'^static/(?P.*)$', 'django.views.static.serve', { 'document_root' : 'C:/Users/Sean/Dropbox/Website/ecomstore/ecomstore/static' }),` – fildred13 Mar 26 '13 at 16:12
  • Have you navigated directly to the css file location? It could be an error message is being displayed. I would also look at removing the os.path.join within the settings.py for the time being and use the full windows path just to rule this out. Finally, I would also check that your view is returning a request context, as this is required to make the static url object populated. – Matt Seymour Mar 26 '13 at 16:17
  • I'm using a variable in my STATICFILES_DIRS so that when I do deploy, I won't have to update that or any other variables along the way. Don't your solution and mine accomplish the same goal in different ways? Or am I missing something? – fildred13 Mar 26 '13 at 16:17
  • Its just ruling out that code is not causing the problem, you can add it in once you know its working. Else print out the os.path.join string and look in console to see what it looks like – Matt Seymour Mar 26 '13 at 16:19
  • This more or less lead me to the answer. You suggestions got me digging in the context processors, which lead me to TEMPLATE_CONTEXT_PROCESSORS, which I then noticed was completely missing the `'django.core.context_processors.static'`. I had indeed overwritten the defaults during my hours of edits, and I just forgot about it in my insomnia. Thanks, Matt! – fildred13 Mar 26 '13 at 16:30
0

The solution is:

{% load static from staticfiles %}
<link rel="Stylesheet" type="text/css" href="{% static "css.css" %}"/>

This is a correct way for call static files on Django 1.7.

chiwangc
  • 3,566
  • 16
  • 26
  • 32
  • Django 1.7 wasn't released when this question was asked, check the date. That said, this may well be the correct way to call static files in 1.7, but even then it doesn't remotely solve the problem, which you can see in the comments of the first answer which explain that it was a bungled context processor. – fildred13 Jan 31 '15 at 07:09