Django React Nginx serving admin static files - python

I feel a bit awkward asking a question which has been answered before - however I feel there is a point of difference with regard to having node serve out the static files as opposed to django. The css bundled by webpack and served by node is working with no problems, where I'm having issues is serving up the admin css and another couple of other files using the get_static_prefix decorator.
The file structure is as follows:
root
|
public
- templates
- static
| <-- collectstatic adding files here
- vendor
|
server
- app1
- app2
| settings.py
/etc/nginx/sites-available/project
server {
listen xxx.xxx.xxx.xxx:8000;
server_name xxx.xxx.xxx.xxx;
location /static {
alias /root/se/env/public/static/;
}
}
and the setup in settings.py
STATIC_URL = '/root/se/env/public/static/'
MEDIA_URL = '/media/'
STATIC_ONLY_URL = '/static_only/'
if not DEBUG:
MEDIA = '/media',
STATIC_ROOT = '/root/se/env/public/static/'
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'public', 'media')
STATICFILES_DIRS = '/root/se/env/public/vendor/static/',
I've been going around in circles for a while now. I'm pretty new to Django and don't really get how this works. From what I can understand the the STATICFILES_DIRS are where collectstatic gathers the static files from and the STATIC_ROOT is where the static files are dumped after collectstatic is run. I think the STATIC_URL is where I'm going wrong - but I'm not having much luck figuring it out.
As it stands the admin css isn't working when DEBUG = True now either, so I've misconfigured something. It's resulting in:
Not Found: /static/admin/css/base.css

Your location /static is wrong. The alias directive substitutes parts of the URI when forming the pathname. The location parameter and the alias parameter should both end with a /, or neither end with a /:
location /static {
alias /root/se/env/public/static;
}
or:
location /static/ {
alias /root/se/env/public/static/;
}
In fact, because the alias parameter ends with the location parameter, you should not be using the alias directive at all. See the note at the end of the alias documentation.
location /static {
root /root/se/env/public;
}

Related

Django/Nginx - 403 on Static Files

I've recently uploaded a django project to an Ubuntu 22.04 DigitalOcean droplet. Nginx, gunicorn, and postgres have all been set up without a hitch, but the static files are all being 403'd.
In both development and production, I never used collectstatic. The reason being, I always put all my static files in a static folder in the root of my project, so I figured I didn't have to do it. I'm not sure if collectstatic needs to be done in order for static files to be shown in production, but it seems that other people have still encountered this problem even when they've done collectstatic.
My project on the server is called pyapps, and in that folder there are the static and media folders. The static folder is further broken down into css, js, and images folders. The media folder is further broken down into photos, then folder by year, month, then day, all of which correlate to the time the photos were uploaded.
Below are my settings.py, urls.py and /etc/nginx/sites-available/ files' code.
settings.py
STATIC_URL = 'static/'
STATICFILES_DIRS = [(os.path.join(BASE_DIR, 'static'))]
MEDIA_URL = 'media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
urls.py
urlpatterns = [
...
] + static(settings.MEDIA_URL, document_root = settings.MEDIA_ROOT)
/etc/nginx/sites-available/Car-terra
server {
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/djangoadmin/pyapps/Car-terra/static/;
}
location /media/ {
alias /home/djangoadmin/pyapps/Car-terra/media/;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
server {
if ($host = www.carterrarfw.online) {
return 301 https://$host$request_uri;
} # managed by Certbot
}
if ($host = carterrarfw.online) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name 146.190.210.138 carterrarfw.online www.carterrarfw.online;
return 404; # managed by Certbot
}
What I've Tried
chmod 777 on the static folder (in retrospect not a good choice, but it didn't work nonetheless)
changing root /home/djangoadmin/pyapps/Car-terra; to alias /home/djangoadmin/pyapps/Car-terra/static/ and media;
changing debug to True just to see if there was a difference (there wasn't)
I'm not sure if this is a problem with collectstatic, or if I'm telling nginx to look in the wrong place.
Is running collectstatic absolutely mandatory?
Thank you for your help.

CSS Not Showing Up in Django with NGINX and uWSGI

This is my first real Django project and I am trying to configure it for production using NGINX and uWSGI. It is running on a Digital Ocean Ubuntu server. Everything is set up and working besides serving static CSS files. The strange thing about this is that its serving static images and JavaScript files fine, the only thing that isn't being served is the CSS.
This is what my site configuration file for NGINX looks like ('nebula' is the Ubuntu User and the name of the Django project):
# configuration of the server
server {
server_name example.com www.example.com;
charset utf-8;
# max upload size
client_max_body_size 75M;
# Django media and static files
location /media {
alias /home/nebula/nebula/media;
}
location /assets {
alias /home/nebula/nebula/assets;
}
# Send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/nebula/nebula/uwsgi_params;
}
}
This is what my Settings.py file looks like:
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_URL = '/assets/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
STATIC_ROOT = os.path.join(BASE_DIR, "assets/")
STATICFILES_DIRS = ( os.path.join(BASE_DIR,'assets/'),)
This is what my base directory looks like (assets and static are the same, I duplicated it in an attempt to solve the issue):
assets demo.py media nebula.sock static uwsgi_params
db.sqlite3 manage.py nebula nebula_uwsgi.ini set store
This is inside of 'assets/':
admin css images jazzmin js vendor
The location path for your static files in your nginx config should match your STATIC_URL setting
location /assets {
alias /home/nebula/nebula/assets;
}
Seems like there could be an issue here
Could you try Changing
STATICFILES_DIRS = ( os.path.join(BASE_DIR,'media/'),)
to
STATICFILES_DIRS = ( os.path.join(BASE_DIR,'assets/'),)

django+nginx deployment not loading static files correctly

server {
listen 80;
server_name 13.xx.xx.xxx;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/studykarma/django-react-redux-base/src/;
}
location / {
include proxy_params;
include /etc/nginx/mime.types;
proxy_pass http://unix:/home/ubuntu/studykarma/django-react-redux-base/src/djangoreactredux.sock;
}
}
my nginx config file
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static_dist'),
)
my settings.py
I have my static files in static_dist folder and my reactjs code in static
folder.
My static files are not loading and giving me 404,
If i change path to /static_dist/ then also i am getting empty content.
I am using this template: https://github.com/Seedstars/django-react-redux-base
About Nginx configuration
location /static/ {
root /home/ubuntu/studykarma/django-react-redux-base/src/;
}
This configuration tells Nginx that on accessing via say example.com/static/app.js, look app.js file inside src folder root, if you want static_dist, then the configuration change will be
location /static/{
root /home/ubuntu/studykarma/django-react-redux-base/src/path/to/static_dist/;
}
About Django configuration
Your Django settings is logically wrong,
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
Above settings tells django that you expect all your static files from STATICFILES_DIRS to STATIC_ROOT, the configuration is effective/used by django when you use collectstatic django manage command, which ineffect copies all your files from STATICFILES_DIRS to STATIC_ROOT( but this also means that, you have to point /static/ location nginx config to static_root instead of static_dist.
Use this,
location /static_root/ { root /home/ubuntu/studykarma/django-react-redux-base/src/static_r‌​oot/; }
it will work

Nginx looking for a different path than what is given Django static files url setting

I ran into very weird issue for which I could not find a reason. I have a django app with uWSGI as my app server and Nginx as our reverse proxy. My initial setting for the static url in Django are as below:
PROJECT_DIR = "/home/ubuntu/src/myapp"
MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_DIR, 'staticfiles')
STATIC_URL = '/staticfiles/'
STATICFILES_DIRS = (
os.path.join(PROJECT_DIR, 'static'),
)
and what's there in the nginx conf is as below:
server {
listen 80;
server_name stg1.myapp.com
client_max_body_size 5G; # adjust to taste
access_log /var/log/nginx/myapp-access.log combined;
error_log /var/log/nginx/myapp-error.log;
location /media {
alias /home/ubuntu/src/myapp/media/; # your Django project's media files - amend as required
}
location /static {
alias /home/ubuntu/src/myapp/staticfiles/;
}
location / {
uwsgi_pass django;
uwsgi_read_timeout 600s;
uwsgi_buffering off;
uwsgi_send_timeout 600s;
proxy_read_timeout 600s;
include /home/ubuntu/src/myapp/uwsgi_params;
}
}
Now, when I was trying to access the server I was getting this error for the static files:
2016/12/13 20:33:03 [error] 30533#0: *194 open() "/home/ubuntu/src/myapp/staticfiles/files/css/base.css" failed (2: No such file or directory), client: 172.31.4.166, server: stg.myapp.com, request: "GET /staticfiles/css/base.css HTTP/1.1", host: "stg.myapp.com", referrer: "http://stg.myapp.com/profile/user1"
Collectstatic copied all the files in the given STATIC_ROOT location. But the path actually searched was - STATIC_ROOT/files.
When I changed the STATIC_ROOT to
STATIC_ROOT = os.path.join(PROJECT_DIR, 'staticfiles/files')
it worked. I am still clueless about why the directory staticfiles/files was being looked for and not just the staticfiles directory as given in nginx conf. Where should I be looking for a possible reason?
EDIT - I had restarted nginx service whenever there was a change done there. So no issues there.
I found the reason.
STATIC_URL = '/staticfiles/' is creating issue. It has to be
STATIC_URL = '/static/'.
The earlier one appends the files in the path on request which was causing my issue.
Cannot find the directory files anywhere in your first set up. [I have not privileges to comment yet].
The set up looks good for me, maybe you just forget to restart Nginx when you modified the configuration file.
Refer link at STATIC_ROOT in Django on Server
Also Static Root and Static Url confusion in Django

my django project static files can not be load with option debug=false

It is very strange that my django website (setup on a linux server with django 1.3) can be visit correctly with DEBUG = True. But when I changed the option to DEBUG = False the static content won't load (css files and images can not be found)
Here's related options i got in my setting.py:
DEBUG = False
STATIC_ROOT = ''
ADMIN_MEDIA_PREFIX = '/static/admin/'
STATICFILES_DIRS = ("/home/ubuntu/ls_website/static/",)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
Anybody have any idea? If the version is too low, I would wait a while for 1.5 release.
I found when debug=False, django won't load static files automatically. Options are set static path in urls.py or serve static files with apache.
People said set static path in urls.py is slower than serve static files with apache. Don't know why though...
Staticfiles doesn't do anything when DEBUG=False. You need to serve those files with apache. Staticfiles has the ability to collect the files to one spot for you to make it easy, then you use some apache magic (assuming here since you didn't specify) to have apache intercept those requests and serve the static files for you.
Alias /robots.txt /home/username/Python/project/site_media/static/robots.txt
Alias /favicon.ico /home/username/Python/project/site_media/static/favicon.ico
Alias /static/ /home/username/Python/project/site_media/static/
I don't remember if it is buildstatic, build_static, collectstatic or collect_static to copy those files from your development stop to your deployment spot, but these variables control how staticfiles does its magic
# Absolute path to the directory that holds static files like app media.
# Example: "/home/media/media.lawrence.com/apps/"
STATIC_ROOT = os.path.join(PROJECT_ROOT, "site_media", "static")
# URL that handles the static files like app media.
# Example: "http://media.lawrence.com"
STATIC_URL = "/static/"
# Additional directories which hold static files
STATICFILES_DIRS = [
os.path.join(PROJECT_ROOT, "static"),
os.path.join(PROJECT_ROOT, "media"),
]
This assumes your static files are in the static folder of your project, and you want to serve them from the site_media folder.
I also move between Windows and Linux for development. To get around the problem of absolute paths in settings.py, I use this function:
def map_path(directory_name):
return os.path.join(os.path.dirname(__file__),
'../' + directory_name).replace('\\', '/')
Which you can implement in this fashion:
STATICFILES_DIRS = (
map_path('static'),
)
This function is tailored for Django 1.4.x. You'll need to modify it slightly for Django 1.3.x. Hope that helps you out.

Categories

Resources