Serve static files with django - 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.

Related

Django static files not loading properly

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

Static and media files are not working on django real server

In the result (real server) the css, js and images are not connected, but the thing is that in localhost it works perfect. I dont know what any other details do you need so write comment and I will edit this queston :)
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
PROJECT_ROOT = os.path.dirname(__file__)
STATIC_URL = '/static/'
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [STATIC_DIR]
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
CRISPY_TEMPLATE_PACK = 'uni_form'
LOGIN_URL = ''
Most probably you misconfigured nginx file or did not mention in nginx file.
You need to write path in nginx file something like below:
location static_url {
alias static_dir
}

Error deploying Django, unable to configure static files

I tried deploying Django project to www.pythonanywhere.com, My website ran but could not load static files.
I did collectstatic command in hosting server console but got error.
my project file configuration as follows
myproject # parent directory
-DjangoApp1 # an app inside project
-Myproject main file(includes manage.py etc)
-DjangoApp2 # an app inside project
-DjangoApp3 (in this app my static files located `DjangoApp3/static/DjangoApp3` )
in settings.py file
STATIC_ROOT = os.path.join(BASE_DIR, "/static/")
STATICFILES_DIRS = ('/DjangoApp3/static/DjangoApp3/',)
STATIC_URL = '/static/'
when I run collectstatic method in local machine ,below error occured
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\DjangoApp3\\static'
This works
STATIC_ROOT = os.path.join(BASE_DIR, "/static/")
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'staticfiles'),)
STATIC_URL = '/static/'
I've covered the three in an answer here before
Try this:
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
(os.path.join(BASE_DIR, 'static')),
)

Static folder in the wrong subdirectory

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

Setting multiple MEDIA_URL & MEDIA_ROOT in django

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