So I'm using a $5 digital ocean droplet using ubuntu and nginix to host a django website, I followed along this this tutorial and this guide. So a part of the guide said to run gunicorn --bind 0.0.0.0:8000 myproject.wsgi
from the virtual env, it worked and as a test I created a highlights object to see if it would show, it did and this is what it looked like:
EDIT 3: Service file
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=apple
Group=www-data
WorkingDirectory=/home/apple/sanskar_handicrafts
ExecStart=/home/apple/sanskar_handicrafts/env/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
sanskar_handicrafts.wsgi:application
[Install]
WantedBy=multi-user.target
After which I added a gunicorn socket and completed the article till the point where it told me to run sudo ufw allow 'Nginx Full'
, however after I ran it, first of all, the css for the admin page was gone but the bigger concern was that after I added a few extra objects from the admin panel the changes didn't seem to be noticed by my views.py as when I displayed the contents of Highlights.objects.all()
it only showed the one from above. But the admin page does show that the objects had been successfully added. Also the github repo's media folder didn't show any of the new images in them including the one uploaded from gunicorn --bind 0.0.0.0:8000 myproject.wsgi
they only had the images I uploaded while in local development
However when I ls
into the media folder from my virtual env running nginix it had all the media files from the images I uploaded while in local as well as all the new ones.
I have 2 models and this problem seems to prevail for both of them, I've tried migrations, restarting the ngnix server, and pulling and pushing to github, none have worked so far. Also the new images do seem to exist in the required url
Here's my models.py:
from django.db import models
class Highlights(models.Model):
img = models.ImageField(upload_to='highlights/')
class Gallery(models.Model):
img = models.ImageField()
and here's my views.py:
from django.shortcuts import render,get_object_or_404
from django.core.mail import send_mail
from .models import Gallery,Highlights
h_images = Highlights.objects.all()
g_images = Gallery.objects.all()
cnt = 1
rows = []
row = []
# Dividing the images into rows of 3 images per row as the maximum and creating a list of those rows
for image in g_images:
if cnt < 3:
row.append(image)
else:
row.append(image)
cnt = 0
rows.append(row)
row = []
cnt += 1
if row:
rows.append(row)
# Seperating the rows into multiple pages to insure faster loading and better optimization
max_rows_per_page = 1
home_page = rows[:max_rows_per_page]
divided_rows_list = []
first = max_rows_per_page
last = max_rows_per_page + max_rows_per_page
curr_page = 1
for i in range(max_rows_per_page, len(rows), max_rows_per_page):
divided_rows_list.append(rows[first:last])
first = last
last += max_rows_per_page
def home(request):
return render(request, 'index.html',{'images':h_images})
def gallery(request):
return render(request, 'gallery.html', {'rows':home_page,'nxt':1,'prev':len(divided_rows_list)})
# return render(request,'gallery.html',{'rows':rows})
def nxt_pg(request,num):
if num > len(divided_rows_list) or num == 0:
return gallery(request)
elif num < 0:
num = len(divided_rows_list)
return render(request, 'gallery.html', {'rows': divided_rows_list[num - 1], 'nxt': num + 1, 'prev': num - 1})
EDIT:
So Ankit told me the problem was because I hadn't run collect static but I had done it so, this is what the static part of my settings.py looks like:
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'static'),
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
this is what I get when I run sudo nano /etc/nginx/sites-available/myproject
EDITED:
server {
listen 80;
server_name 139.59.20.1;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
alias /home/apple/sanskar_handicrafts/staticfiles;
}
location /media/ {
root /home/apple/sanskar_handicrafts;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
Also I do have a static_root folder and this is what happens when I try to use collectstatic now:
So I don't think it's a problem with collectstatic.