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
Related
I did run the python manage.py collectstatic code, and after that all the static files were collected in the static folder. but the static files needed for the syling of the admin panel are not working. Could you help?
settings.py
STATIC_URL = 'static/'
STATICFILES_DIRS = [
'Portfolio/static/',
'Portfolio/',
''
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
Comment out the STATIC_ROOT
I think your STATICFILES_DIRS is wrong change it to os.path.join(BASE_DIR, 'static')
Try to change STATIC_URL. for example: /Portfolio-Deployment-Folder/static/
Try to change localhost port for example:
python3 manage.py runserver 9000 or 7000
most probably problem of STATICFILES_DIRS
And finally a full example for STATIC in my personal project:
STATIC_URL = '/myproject/static/'
# STATIC_ROOT = BASE_DIR / 'static'
STATICFILES_DIRS = [
BASE_DIR / 'static'
]
I tried to get my project set up with static files and I do not seem to be successful. I want a global static files instead of an in-app one and so my settings.py is
STATIC_URL = '/static/'
STATICFILES_DIRS =[os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
And as such my directory is
The issue I face is that my vendor folders will not load. Only images do and my custom css file.
What I did is move animate.min.css to the css folder and it worked but I wonder if being inside many folders affects it.
root urls.py
urlpatterns += static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] # It will be deactivated when running collectstatic. Active on localhost
#STATIC_ROOT = os.path.join(BASE_DIR, 'static/') # It will be activated when running collectstatic. Active on server
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
I am new to Django and I am trying to setup a static folder for my project.
I am trying to place the static files in the following folder:
/portfolio-project/portfolio/static
I have made the following additions to settings.py:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'portfolio/static/'),
]
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')
STATIC_URL = '/static/'
However, after running collectstatic the static folder is created in: /portfolio-project/
You can customize STATIC_ROOT if you want to.
in your case
STATIC_URL = '/portfolio/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'portfolio/static/')
I've set Static and Media root as well as url's in my django app, as follows:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
MEDIA_URL = '/crl/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'config/crl/')
It is working great, but I want to add another MEDIA_URL & MEDIA_ROOT to serve files from the /certs/ directory as follows:
NEW_MEDIA_URL = '/certs/'
NEW_MEDIA_ROOT = os.path.join(BASE_DIR, 'config/certs/')
Is there any way to do it?
I'm using Django 2.0.6 and Python 3.5
Multiple static URLs and static roots can be added to Django using the following steps.
Configure a BASE_DIR
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
Create as many static roots and static URLs as you need
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
# the "static/" above is a directory inside the Django Project Directory
STATIC_URL_1 = '/static-1/'
STATIC_ROOT_1 = os.path.join(BASE_DIR, "static_1/")
Similarly, you can create as many media roots and media URLs as you need
MEDIA_URL = '/crl/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'config/crl/')
MEDIA_URL_1 = '/crl-1/'
MEDIA_ROOT_1 = os.path.join(BASE_DIR, 'config/crl_1/')
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.