Missing files in Django admin dashboard - python

I'm a newbie in Django and I have this problem.
When I enter to my admin dashboard, CSS and JS files aren't shown. When I follow the URL, it appears
404 Not Found
I'm using Nginx + Gunicorn.
This is my Nginx configuration:
server {
server_name MYPROJECT.COM;
access_log off;
location /static {
alias /opt/myenv/myenv/MYPROJECT/static/;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
Also this is settings.py static configuration:
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, "static")
My css admin files are stored in/opt/myenv/myenv/MYPROJECT/static/admin
, but also in /opt/myenv/myenv/static/admin
I've uploaded the files in both directories and run collectstatic
Can anyone help me?
Thanks, everyone!

We had issues getting collectstatic working with admin in an older version of Django, and fixed it in our nginx config by doing something like
location /static {
root /path/project;
}
location /static/admin {
root /path/venv/lib/python3.4/site-packages/django/contrib/admin;
}

Related

Cannot find Django admin static file (404 not found)

My Django project deployed on EC2 instance. Everything is fine except static files. Here is my project directory path tree
my_project
└─admin
├── admin
│   ├── admin
│   ├── keywords
│   ├── manage.py
│   └── static
├── README.md
And this is my settings.py setting code about staticfiles
# settings.py
debug=False
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
ROOT_DIR = os.path.dirname(BASE_DIR)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'my_project/static')
STATICFILES_DIRS = [
STATIC_ROOT
]
And this is my nginx.conf files
root /usr/share/nginx/html;
location = /favicon.ico { access_log off; log_not_found off; }
location /static {
alias /static/;
}
location / {
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://unix:/home/devel/run/gunicorn.sock;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
And also my machine's environments:
Centos 7.9
python 3.6.8
Django 4.0.6
I already did python manage.py collectstatic command.
When I enter into this project's Admin page, Every UI have loaded without CSS and JS though.
Is somewhere of my setting is wrong? I wonder why django cannot find any static files.
Assuming your BASE_DIR is the same as /usr/share/nginx/html, and the BASE_DIR/myproject/static exists and has content, try
location /static/ {
root /myproject/;
}
(Using root rather than alias as the end directories are the same, as per nginx docs)
I am assuming your earlier root definition will prefix the location - if not you might have to use root /usr/share/nginx/html/myproject/ explicitly instead

Django + nginx + gunicorn not being able to serve static files

I'm trying to deploy a django app in a EC2 instance but I'm currently having issues serving the static files of the app.
Currently, my nginx's conf file looks like this:
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://0.0.0.0:8000;
}
location /static/ {
#autoindex on;
root /home/ubuntu/some/folder/static/;
}
}
Also in my settings.py file in my django project I have the following config related to my static files:
STATIC_URL = '/static/'
STATIC_ROOT = '/home/ubuntu/some/folder/static/'
and Gunicorn, I'm launching it as the following:
gunicorn3 --bind 0.0.0.0:8000 myproject.wsgi:application
And with that, if I access http://ec2-my-instanceI see the html content, but all my js and css files get error 404.
I'm kinda lost and I don't know what I'm missing to make it works, I already migrated the static content and I checked the folder which nginx is looking for the static files have them, but if I check the browser's console I see this kind of errors:
GET http://ec2-my-instance/static/somefile.js net::ERR_ABORTED 404 (Not Found)
You want 'alias' not 'root':
location /static/ {
alias /home/ubuntu/some/folder/static/;
}

After I distribute my Django/Django-Rest-Framework project, I can not request its `media` and `static` directory

I have a Django/Django-Rest-Framework project, and it only provides the APIs.
This is one of the drf APIs, I request the remote ipaddress in the browser:
I can not request its static resources.
I am not sure whether when Django APIs distribute, I will config the nginx for static and media placement.
If need Nginx config for Django APIs static resources, How can I config it?
this is an example of nginx vhost configure file:
server {
listen 80;
server_name www.abc.xyz;
access_log logs/www.abc.access.log main;
location / {
root /var/www/html/website/;
index index.html index.htm;
}
location ~ /media/*\.(jpg|png|jpeg|bmp|gif|swf)$
{
access_log off;
expires 30d;
root /var/www/html/python_backend/abc;
break;
}
location /api/ {
proxy_pass http://101.20.32.76:8000/api/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /media/ {
proxy_pass http://101.20.32.76:8000/media/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
You see the localtion /:
location / {
root /var/www/html/website/;
index index.html index.htm;
}
because my Django/Django-Rest-Framework is only provide APIs, there is no such index.html files, so I don't know how to config it.
and the http://101.20.32.76:8000/media/ I can not access, so the configuration of location /media/ and location /static/ is invalid right?
EDIT-1
I deploy in my remote server (CentOS7.2)
and in my remote repo, there are static directory and media directory, because I pushed them from local repo, they are not in .gitignore.
EDIT-2
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = (
)
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
this is the configuration of my settings.py.
During your application deployment; you'll need to run python manage.py collectstatic
https://docs.djangoproject.com/en/2.0/ref/contrib/staticfiles/#collectstatic
Collects the static files into STATIC_ROOT.
Files are searched by using the enabled finders. The default is to
look in all locations defined in STATICFILES_DIRS and in the 'static'
directory of apps specified by the INSTALLED_APPS
Read this for more info on how and when to collect static and how to serve static assets with Django:
https://docs.djangoproject.com/en/2.0/howto/static-files/deployment/#serving-the-site-and-your-static-files-from-the-same-server

'Welcome' message from Nginx shown when I run my wsgi Django project + Nginx + Gunicorn

I'm beginner in Django and I've finished my first project.
I have an Ubuntu server in Digital Ocean and this is what I've done:
My project nginx configuration file:
server {
server_name domain.com;
access_log off;
location /static/ {
alias /opt/myenv/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
My project is located in /opt/myenv/myenv/
When I execute gunicorn myproject.wsgi it looks like it's running
Listening at: http://127.0.0.1:8000 (1481)
But when I access into my IP, I just see an Welcome message from Nginx.
What is happening?
(Sorry my bad english)
I can't comment (rep points) so will post here.
You have Gunicorn bound to :8000 but have Nginx looking at :8001
Did you put this config in sites-available and symlink to sites-enabled, then remove "default" from sites-enabled? (If it's showing welcome I assume not)
I don't see where you are directing Nginx to listen:
server {
listen 80 default_server;
listen [::]:80;
...
Any reason you are not setting up Gunicorn using a unix sock instead and configuring Nginx to that? Here's a typical config for this:
server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/user/myproject/myproject.sock;
}
}

Django using Nginx to serve static content

I am trying to configure nginx to serve the static content for my django project on a remote VPS. I'm using the following configuration for my nginx instance:
server {
server_name myVPSip;
access_log off;
location /static/ {
alias /usr/local/pcat/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
I created the config file in ../nginx/sites-available/, linked it to /sites-enabled/, and restarted nginx however when I hit myip:8001/static I get a django 404.
You shouldn't access your website by 123.123.123.123:8001 because that's the port django is using. You are supposed to access 123.123.123.123 (that is port 80) where nginx is running.

Categories

Resources