django making deeper static root on every deploy - django

I have django setup to set the static root inside a static folder in the project directory, but every time I deploy it is adding another layer of root folders for the static files. I can't figure out why this is happenning...
+ project
-app1
+static
+root
+root
+root
+root
-templates
-manage.py
Here is the settings for static...
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'root')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
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(BASE_DIR, 'static'),
os.path.join(BASE_DIR, 'main', 'static')
)
AND NGINX...
# your Django project's static files - amend as required
location /static {
alias /home/django/langalang/static/root;
}

Your static root is included in your staticfiles_dirs, so every time Django runs collectstatic it puts it inside itself. Don't do that: keep them separate.

Related

Django created staticfiles folder rather than static folder

I do not know why Django created a folder named staticfiles rather than static as expected. That might be the reason why I got an error after running python manage.py collectstatic:
The system cannot find the path specified: 'D:...\\static'
My settings file included:
from pathlib import Path
import os
import django_heroku
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
I had already tried to change 'staticfiles' to 'static' in STATIC_ROOT, but no success.
Could anyone please explain why and how to create a folder name "static"?
Thank you very much in advance!
I considered your static files are placed inside your app(s)
Try removing STATICFILES_DIRS so the setting will be:
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
...and try to re-run python manage.py collectstatic
Here is how collectstatic works:
First it collect static files from STATICFILES_DIRS if any, if there is no STATICFILES_DIRS in settings it collects static files from each app
Then placed them to STATIC_ROOT, so if your static files are placed inside your app(s) better to remove STATICFILES_DIRS
If still there is an error share your project structure

Serve static files with django

Setting up a django site but I can't get it to serve static files.
In the settings file
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)
TEMPLATE_PATH = os.path.join(PROJECT_PATH, 'templates')
STATIC_ROOT = os.path.join(PROJECT_PATH, 'angular/js')
STATIC_URL = '/static/'
In the template
<script src="/static/js/angular.min.js"></script>
I can serve the template which is in the main directory angular and inside it is the static folder angular/static/js/angular.min.js
Add following code in setting.py file
STATIC_ROOT = os.path.join(BASE_DIR,"deploy_to_server")
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
And use in your template
<script src="/static/js/angular.min.js"></script>
Create static folder in your project
==> static ==> js ==> angular.min.js
==> manage.py
Hope this is help you
Dont make STATIC_ROOT specific for an app. It should be of whole project scope.So my recommendation
STATIC_ROOT = os.path.join(PROJECT_PATH, 'static')
STATIC_URL = '/static/'
Then add an app named angular as any other app.
Add to INSTALLED_APPS
create folders static/angular/js inside it like
angular/static/angular/js
Copy angular.min.js inside it as
angular/static/angular/js/angular.min.js
access it via src=/static/angular/js/angular.min.js
So there will not not be much change in production also.

Static don't load in Django CMS

Static files don't load after I moved them to another directory.
My settings.py looks like this:
DATA_DIR = os.path.dirname(os.path.dirname(__file__))
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(DATA_DIR, 'static')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'src', 'static'),
)
STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder"
)
INSTALLED_APPS = (
...
'django.contrib.staticfiles',
...
)
All the static I want to place in the root directory of my virtualenv. There are folders
src(root_dir)-->media /* uploaded media */
-->static /* for css, js etc*/
-->project_y /*web-page*/
-->templates /*html templates */
The strange thing (for me) is that templates and media works normally from that directories. I've placed them this way:
MEDIA_URL = '/media/'
TEMPLATE_DIRS = (
os.path.join(BASE_DIR, 'templates'
)
Any ideas how to relocate that static?
For Static Files, you have to collectstatic
python manage.py collectstatic
Will collect files from all the STATICFILES_DIRS and place them inside the STATIC_ROOT. Only after this, the static files can be accessed

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.

Django Static Files Not Serving On Development With Proper Config

I have my project laid out like this:
Project
App1
App2
My static folder is in Project with 2 directories like so:
-/Project/static/css
-/Project/static/js
I have the following config in my settings.py file:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
...
urlpatterns += staticfiles_urlpatterns()
However I am getting 404s on any files served from there. I have 'django.contrib.staticfiles', in my INSTALLED_APPS constant and my static constants set like:
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
MEDIA_ROOT = PROJECT_PATH + '/media/'
MEDIA_URL = '/media'
STATIC_ROOT = PROJECT_PATH + '/static/'
STATIC_URL = '/static'
What could I be doing wrong? I get nothing but 404s and it is driving me crazy.
I was setting STATIC_ROOT and MEDIA_ROOT when I should have been setting this:
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.
PROJECT_PATH + '/static/',
)