django-staticfiles breaking the admin interface - django

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.

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 collected static files are not accessible by templates

I've created a django project with some apps. Initially, I just had a single app and i had static files and templates in it. As my project grew, I added some other apps to the project which they are still accessing the statics files and templates from inside the main app. I wasn't giving much care to this problem until i tried to make a simple production and used collectstatic command. Seems i can collect my static files to the STATIC_ROOT directory but i can't access them in the templates. Here is my project structure:
shop/
-myshop(main app)
--statics
--templates
-(some other apps beside main app)
-shop(directory created by project containing settings.py, manage.py)
This is my relevant settings.py configurations:
BASE_DIR = Path(__file__).resolve().parent.parent
INSTALLED_APPS = [
...
'django.contrib.staticfiles',
...
]
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / 'myshop/static',
]
STATIC_ROOT = BASE_DIR / 'static'
After running collectstatic django creates the static directory in root of project but when i remove or rename the myshop/static i get static files 404 in runserver.
I dont know how to check {% load static %} resulting path in runtime to put more information here. Is there any debug routine to understand what's django backend doing?
Try this
STATIC_URL = '/static/'
STATICFILES_DIRS = [
BASE_DIR / "static",
]
STATIC_ROOT = BASE_DIR.parent / "static_cdn"
$ python manage.py collectstatic This command will copy all files from your static folders into the STATIC_ROOT directory.
You must write the names of the static folder and the static root folder diffrent.
To learn more https://docs.djangoproject.com/en/3.1/howto/static-files/#configuring-static-files

Django Collectstatic with AWS S3 not copying correct static and media files

I am trying to copy all of my static and media files to an S3 bucket. Unfortunately despite the fact that my media root and static root are provide in my settings.py file....
DEFAULT_FILE_STORAGE = 'jeffrey.aws_storage_classes.MediaStorage'
AWS_ACCESS_KEY_ID = 'keyhere'
AWS_SECRET_ACCESS_KEY = 'secretkeyhere'
AWS_STORAGE_BUCKET_NAME = 'bucketname'
STATICFILES_STORAGE = 'jeffrey.aws_storage_classes.StaticStorage'
AWS_S3_DOMAIN = "%s.s3.amazonaws.com" % AWS_STORAGE_BUCKET_NAME
STATIC_URL = "https://%s/static/" % AWS_S3_DOMAIN
MEDIA_URL = "https://%s/media/" % AWS_S3_DOMAIN
MEDIA_ROOT = u'/home/namehere/mysite/media'
STATIC_ROOT = u'/home/namehere/mysite/static'
....files from my django contrib folder seem to be being copied, and my files in static and media folders are not. Below is my bash console:
Type 'yes' to continue, or 'no' to cancel: yes
Copying '/usr/local/lib/python2.7/dist- packages/django/contrib/admin/static/admin/css/fonts.css'
Copying '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/img/icon-addlink.svg'
Copying '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/img/icon-no.svg'
Copying '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/img/inline-delete.svg'
....
....
Copying '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/img/gis/move_vertex_on.svg'
Copying '/usr/local/lib/python2.7/dist-packages/django/contrib/admin/static/admin/img/gis/move_vertex_off.svg'
61 static files copied.
I think the problem lies with your INSTALLED_APPS setting.
It is not wrong for it to copy files from django contrib folder - if you have 'django.contrib.admin' inside of your INSTALLED_APPS setting then it will copy the static files for this such as you have listed above. (This is the Django admin site).
If you don't have your app listed in INSTALLED_APPS then it will skip over collecting static files from your app's folder. I managed to reproduce your problem exactly by removing my app name from this setting, and without a doubt, suddenly it only copied files from django contrib and skipped over my apps static files.
So even if your problem turns out to be different, this will have same effect as you described.
Example: In settings.py you need to ensure you have something that looks like this:
INSTALLED_APPS = [
# Add your apps here to enable them
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'storages', #Amazon S3
'myapp',
]
Note that 'myapp' will be whatever the name of your app is.

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'),
)

Serving django static files in development envirnoment

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/