How to serve static files for local development in Django 1.4

I just downloaded the latest version of Django (1.4.1), and I can’t figure out how to serve css files when developing locally using a server server. I read the corresponding Django docs on static files , and there are many, many questions and answers ... it looks like it should be more or less automatic, but it does not work for me.

I am working on a survey application from a textbook.

404 from the magazine

[27/Apr/2012 01:04:09] "GET /polls/ HTTP/1.1" 200 210 [27/Apr/2012 01:04:09] "GET /polls/css/styles.css HTTP/1.1" 404 2596 

Directory structure

 mysite |-- manage.py |-- mysite |-- __init__.py |-- settings.py |-- urls.py |-- wsgi.py |-- polls |-- __init__.py |-- models.py |-- tests.py |-- views.py |-- static |-- css |-- styles.css |-- templates |-- polls |-- index.html 

index.html

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

settings.py

 MEDIA_ROOT = '' MEDIA_URL = '' STATIC_ROOT = '' STATIC_URL = '/static/' STATICFILES_DIRS = () STATICFILES_FINDERS = ( 'django.contrib.staticfiles.finders.FileSystemFinder', 'django.contrib.staticfiles.finders.AppDirectoriesFinder', ) TEMPLATE_CONTEXT_PROCESSORS = ( "django.core.context_processors.auth", "django.core.context_processors.debug", "django.core.context_processors.i18n", "django.core.context_processors.media", 'django.core.context_processors.static', ) 

^^^ I did not have the TEMPLATE_CONTEXT_PROCESSORS variable in settings.py when I started the project and had to add it manually - should I worry about that?

STATICFILES_DIRS is empty because the css file is located in a directory called static in the polls application in which Django automatically searches for it - right?

I also have django.contrib.staticfiles in my INSTALLED_APPS.

urls.py

I saw in the docs that this solution works for local development servers other than runerver - it doesn't seem like it should be otherwise, right? (I currently commented on it.)

EDIT: I uncommented these lines, but didn't see the changes - I still get the same 404 in the css file

 from django.contrib.staticfiles.urls import staticfiles_urlpatterns urlpatterns += staticfiles_urlpatterns() 

Is my directory structure incorrectly configured? Am I missing the necessary settings in settings.py? Any help would be much appreciated! Thanks!


EDIT:

I made a Mark suggestion and read in RequestContext. Change my view:

 return render_to_response('polls/index.html', {'latest_poll_list': latest_poll_list}) 

to

 from django.template import RequestContext ... return render_to_response('polls/index.html', {'latest_poll_list': latest_poll_list}, context_instance=RequestContext(request)) 

received / static / url for registration:

 [27/Apr/2012 13:56:55] "GET /static/css/styles.css HTTP/1.1" 200 19 

This fixes the problem.

+7
source share
4 answers

To use STATIC_URL in a template, you must be sure to use RequestContext along with adding 'django.core.context_processors.static' to TEMPLATE_CONTEXT_PROCESSORS . This is done for you if you use the render shortcut. If you are not using RequestContext , you can use the template tag {% get_static_prefix %} in the staticfiles template tag library. This is described in detail in the docs: https://docs.djangoproject.com/en/1.4/ref/contrib/staticfiles/#other-helpers

+10
source

. I had the same problem, and, of course, with the same combination as mentioned above, this did the trick: I added the setup path to my static directory in STATICFILES_DIRS:

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

when also in settings.py PROJECT_DIR is set to:

 PROJECT_DIR=os.path.dirname(os.path.abspath(__file__)) 
+1
source

From your 404, it looks like you are not including {{STATIC_URL}} in your template. This is probably a problem. instead of "css / file", "{{STATIC_URL}} css / file"

Without looking too hard, your settings look great. If the above does not work, try starting. /manage.py collectstatic and see if this helps (it moves static files from application directories to a static project directory).

And definitely uncomment these lines in urls.py! If you don't have static url patterns when it asks for something from / static /, it does not know what to do.

0
source

This worked for me on the development server:
1. I added a "static" folder in my "myblog" application.
2. Added the file "myblog.css" inside the "static" folder.
3. Link the stylesheet to my application template, code below.

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

I did not change the settings.py or urls.py files to fix css, the changes were made only in accordance with the tutorial, so you should have the same.

0
source

All Articles