Serving django static files in development envirnoment - django

I want to use static files to load my css with runserver command. The problem is that i tried all the solution i found here on stackoverflow and also in django docs, but it doesnt works at all... I dont know what can i do...
If i set
STATIC_URL = '/static/'
STATIC_ROOT = 'C:\Users\Max\Works\www\mysite\mysite\static'
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
)
I thought it was enough... Am i missing something?
Can you tell me what is the best setting for have static files in develompent envirnoment?
Thanks in advice...
EDIT(1)
I already putted in my template {{ STATIC_URL }}css/dark-grey.css" and ofc the css is in C:\Users\Max\Works\www\mysite\mysite\static\css\dark-grey.css, i really can't get what is wrong...

Use / slashes and NOT \ slashes in the path, even for windows paths.

In your settings.py
DEBUG=True
As per the docs:
This view is automatically enabled by runserver (with a DEBUG setting
set to True).
Using the URL pattern is a way to force it, which I personally don't even have to do in my project as long as DEBUG=True. You would always have DEBUG on when you are developing, and when you switch to production you aren't even using the development server anyways, so you would be pointing your production server to the static location.
This is a snippet of my static settings from my settings.py. I do not manually have to add that static view URL
import os
DEBUG = True
PROJECT_ROOT = os.path.dirname( __file__ )
PROJECT_NAME = os.path.basename(PROJECT_ROOT)
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/')
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(PROJECT_ROOT, 'web/'),
)
# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
TEMPLATE_CONTEXT_PROCESSORS = (
...
'django.core.context_processors.static',
...
...
)

You need to add url patterns:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
# ... the rest of your URLconf here ...
urlpatterns += staticfiles_urlpatterns()
See the documentation here

You also need to run the following command to get the static files moved into the right place (and for Django to know they're there):
python manage.py collectstatic
Full documentation on static files in Django 1.3 is here:
https://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/

Related

Can't load static files in my django project, error 404

I'm new to Django and i've seen a lot of people asking about this particular problem but i followed every instruction i could find and it still doesn't work.
My directorys look like this:
/Peperina
/Writing
/statics/writing
/css
/scss
/js
forms.js
/templates/writing
forms.html
my setings.py:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
(...)
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'writing.apps.WritingConfig',
]
(...)
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
STATIC_URL = '/statics/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'statics'),
)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
My urls.py:
(...)
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
finaly my forms.html
(...)
{% load static %}
<script src="{% static 'writing\js\forms.js' %}"></script>
yet 404 keeps showing up
Try to change your STATIC_ROOT to os.path.join(BASE_DIR, 'statics') ?
I have had this same problem multiple times myself.
I'd first start by removing the "writing" directory, and move all folders up.
You won't need to change your settings.py file too much if you start by doing that.
Create a new directory, called "static_root" in the same level as your current "statics" folder and copy all the static files you've got in your statics folder over to there. Be sure to have two copies of each (one in "statics" and another in "static_root"). This will be explained shortly.
Your static file information in settings.py should now be changed to look like this:
STATIC_ROOT = os.path.join(BASE_DIR, 'statics')
STATIC_URL = '/statics/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static_root'),
)
The reason you need to have a separate static_root is because Django sometimes gets a bit funny about there being a root static folder and a normal static folder. More info can be found here.
I personally do not have STATICFILES_FINDERS in my settings.py, so test with removing that.
Maybe also try running Python manage.py collectstatic as this will collect the static files for you.
If none of this works, as you mention you are new to Django, I'd recommend following some tutorials from start to finish to see if you can get it to work.
E.g. start here if you haven't done already.
Well this turned up to be a pretty goofy solution but i changed the name of all my "statics" directorys to just "static" and then did the same thing in my settings and... it worked
So... thanks anyway

Django static files getting 404

Hello and thanks for your help in advance. I realize this question has been asked and answered in other placed but none of those answers are working for me.
I am new to python and django and have inherited a small webapp. I have a dev environment working on my computer with mostly unchanged code, the only changes being to the database name and password to point to my local mySQL server.
However, when I run the app, everything works except for the static files. I'm getting 404s in the console when trying to retrieve static files and js methods in static are coming up undefined.
The BASE_PATH, STATIC_URL, STATIC_ROOT, STATICFILES_DIR, STATICFILES_FINDERS are all unchanged from the currently working production code, and as far as my beginner eyes can tell are configured correctly according to the documentation and the multiple answers to this question.
Is there something that could be different about what I have installed on my computer that would be causing this? Why else would it be different between production and my local copy? Is there something I have to run to get this working?
Some settings in settings.py:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
INSTALLED_APPS = [
'biogen.apps.BiogenConfig',
'msm.apps.MsmConfig',
'tracker.apps.TrackerConfig',
'accounts.apps.AccountsConfig',
'process_manager.apps.ProcessManagerConfig',
'process.apps.ProcessConfig',
'pfd.apps.PfdConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'djangobower',
'rest_framework',
'crispy_forms',
'django_summernote'
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIR = [
os.path.join(BASE_DIR, 'components/static'),
]
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'djangobower.finders.BowerFinder',
]
Included in the templates:
{% load static %}
Thanks again in advance...
Provide something like this:
Consider you have in your project root directory static_files as a source and static as a destination of your static files - you initially place and edit them in static_files (or any other you have):
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static_files'),
os.path.join(BASE_DIR, 'static_files/js'),
os.path.join(BASE_DIR, 'static_files/html'),)
STATIC_ROOT = os.path.join(BASE_DIR, 'static', )
STATIC_URL = '/static/'
Notice it's STATICFILES_DIRS, not STATICFILES_DIR
Then run
python manage.py collectstatic
to collect your static files from static_files to static
Turns out I needed to load all the static files from Bower. All these files are present on production but not on development for some reason!
Try running
python manage.py collectstatic

Heroku django - messed up project structure

I've deployed my app on heroku (cedar stack) and all worked fine until a few days ago, When i noticed that my static files are not being served (appear with code 'canceled'). Next thing i noticed is that my project suddenly had new folders on same level as manage.py : admin/, css/, img/ and js/. I didnt create admin at all, using a buildin django admin site and didnt modify it in any way. I did create css, img and js folders under static/ inside my project, and its still there along with all its content. It's like all the folders in static/ got copied to manage.py level.
Running heroku run ls -l shows same changes on heroku. I suspect those changes cause troubles in serving static files.
heroku run python manage.py collectstatic --noinput
shows all files copied no problem.
my static dirs settings:
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
os.path.join(os.path.dirname(__file__), 'static'),
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
I dont use S3 yet.
How to remove those files? What caused those dirs to appear?
When a Django application is deployed to Heroku, collectstatic is automatically run (Heroku docs). This collects all the static assets from your installed apps (including django.contrib.admin) and copies them to the STATIC_ROOT.
Check your STATIC_ROOT setting. It is probably not set correctly.
Here is a tutorial on using S3 with your static files.
Based on this post i learned that all this mess was caused by changing DEBUG to FALSE. Following Ohads' advice i checked my STATIC_ROOT setting, and as you can see, it wasn't set. Those are my fixed settings:
SETTINGS_PATH = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT = os.path.abspath(SETTINGS_PATH+'/static/')
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
Adding:
if not settings.DEBUG:
urlpatterns += patterns('',
(r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
)
in myapp/urls.py solved my problem.

A simple static file location issue

I've gone and adjusted something somewhere along the line, and cant find way back.
My file structure
examplesite1
--registration
--A bunch of files well ignore
--snapboard
--static
--Associated static folders css, js etc
--templates
--snapboard
--html files
--some folders
--manage.py
--settings.py
--urls.py
--__init__.py
Im having trouble pointing the html files in templates/snapboard to the static files in snapboard/media.The code in the html is
<link type="text/css" rel="stylesheet" href="{{ SNAP_MEDIA_PREFIX }}/css/yui/reset-fonts-grids.css" />
in the settings file I have of that which i think is valid
MEDIA_ROOT = ''
MEDIA_URL = '/snapboard/'
ADMIN_MEDIA_PREFIX = '/media/admin/'
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
#'django.template.loaders.eggs.load_template_source',
)
STATICFILES_DIRS = (
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'django.contrib.staticfiles',
# 'django.contrib.staticfiles.finders.DefaultStorageFinder',
)
STATIC_URL = 'static/'
TEMPLATE_CONTEXT_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.request",
"snapboard.views.snapboard_default_context",
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
'pagination.middleware.PaginationMiddleware',
'snapboard.middleware.threadlocals.ThreadLocals',
'snapboard.middleware.ban.IPBanMiddleware',
'snapboard.middleware.ban.UserBanMiddleware',
)
ROOT_URLCONF = 'examplesite1.urls'
TEMPLATE_DIRS = (
)
INSTALLED_APPS = (
'examplesite1',
'registration',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.markup',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.staticfiles',
'snapboard',
'pagination',
)
SNAP_MEDIA_PREFIX = MEDIA_URL + 'static/'
# Set MEDIA_ROOT so the project works out of the box
import os
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'snapboard/media')
Ive spent bloody ages going through a process of elimination trying a ton different combinations with out success. Whats the correct way to code this.
make ur media url as MEDIA_URL = '/snapboard/' and in your code change href="{{ SNAP_MEDIA_PREFIX }}/css/yui/reset-fonts-grids.css" to href="{{ SNAP_MEDIA_PREFIX }}css/yui/reset-fonts-grids.css" because {{ SNAP_MEDIA_PREFIX }}/css/ is not same as {{ SNAP_MEDIA_PREFIX }}css/
What version of Django are you running? If it's 1.3+, you should be using the staticfiles contrib package and you app's "media" directory should be named static to allow Django to pick it up.
Otherwise, there's no support for static resources inside your apps, so you need to copy all those files into your MEDIA_ROOT directory.
UPDATED
Drop the whole SNAP_MEDIA_PREFIX business; it's unnecessary and complicating things. This is all you need:
STATIC_ROOT = os.path.join(os.path.dirname(__file__), 'static')
STATIC_URL = '/static/'
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'site_media')
MEDIA_URL = '/site_media/'
Never directly use STATIC_ROOT. This is the directory where the collectstatic management command will dump files, and should be served directly by your production webserver. Django never serves anything from here.
MEDIA_ROOT is for uploads, now, i.e. any time you have a FileField, ImageField, etc. on a model, the actual file will be put somewhere in here. It also should be directly served by your webserver in production. Don't put any of your static resources here.
Do put all of your static resources in your app's static directory. Each app's static directory will be served by Django in development under STATIC_URL. So, <project_root>/some_app/static/style.css will be available at /static/style.css through your browser. Because of this, it's usually a good idea to namespace your static directories, i.e. use <project_root>/some_app/static/some_app/style.css instead so that the URL becomes /static/some_app/style.css. That way your static files don't overwrite each other.
If you need project-wide static resources, create an entirely different directory in your project (I tend to use assets) and then add that to STATICFILES_DIRS like so:
STATICFILES_DIRS = (
os.path.join(os.path.dirname(__file__), 'assets'),
)

django-staticfiles breaking the admin interface

I'm using the django-staticfiles app to serve css files, but this also prevents required admin css files (media/base.css, media/dashboard.css) from being loaded. It seems like I need to exclude the admin app, but adding it to the STATICFILES_EXCLUDED_APPS hasn't helped.
Here are the relevant bits from my settings.py file:
ADMIN_MEDIA_PREFIX = '/media/'
...
STATIC_URL = '/static/'
STATIC_ROOT = ''
STATICFILES_EXCLUDED_APPS = (
'django.contrib.admin',
)
INSTALLED_APPS = (
...
'django.contrib.admin',
'staticfiles',
)
(I'll assume this is for development as you shouldn't serve static content with django-staticfiles.)
You have to define the ADMIN_MEDIA_ROOT variable in your settings.py file and point it to the location of the admin css files. I've moved these files out of site-packages to be in a similar location as my other static files for ease of deployment.