Static and media files are not working on django real server - django

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
}

Related

What's the difference between MEDIA_ROOT = BASE_DIR / 'media'` and MEDIA_ROOT= os.path.join(BASE_DIR, "media") in Django setting.py

I wanted to know the difference between using MEDIA_ROOT = BASE_DIR / 'media' and MEDIA_ROOT= os.path.join(BASE_DIR, "media") in settings.py.
Those are basically the same, but only if BASE_DIR is a pathlib.Path object.
MEDIA_ROOT = BASE_DIR / 'media'
utilizes pathlib library
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
utilizes os library
The default library depends on which version of Django you employ.
What do you mean by deleting and uploading can you give more detail?

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

Gunicorn cant find the static files

By using gunicorn without nginx from digitalcloud tutorial
my server is runing and on the console is
not found: /static/style.css
settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
I already tried to
collectstatic
do urlpatterns += staticfiles_urlpatterns() in urls.py file
makemigrations + migrate
maybe django try read 'static//static/style.css' but you need this: 'style.css' ;) ?
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
urlpatterns = [your urls here] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
You can create in your project folder settings then in them create settings files like names:
local.py
from my_project.settings import *
DEBUG = True
ALLOWED_HOSTS = []
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
this run in VM like: python manage.py runserver --settings settings.local
remember in urls add:
if settings.DEBUG == True:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
web.py
from my_project.settings import *
DEBUG = False
ALLOWED_HOSTS = ["www.side.com", "side.com"]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
and then in gunicorn import only web.py file project to read in nginx
this run in VPS server.

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

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.