0

I am working with a demo Django project of video blog that I have hosted on a local Ubuntu Server 18.04 LTS with Gunicorn, Nginx, Redis and Postgresql. I have installed this server on Virtualbox and assigned a fixed IP. Static files (html, css, js) are working well but video and a default image file of media folder are not showing. Both static and media folders are in the project root directory. I think have not configured properly the Nginx configuration file. But everything works fine when I check them with development server. I'm very new my dear and have a lack of advance programming knowledge. That's why, I am asking for your help for solving this problem. Please have a look the code bellow if there is any mistake. Thanks in advance!

Error from browser console

Failed to load resource: the server responded with a status of 403 (Forbidden) /media/videos/2019/10/02/Algebra_Basics-_What_Is_Algebra_-_Math_Antics.mp4:1

Project structure

enter image description here

part of settings.py file

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/

STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'kiji/static')
]    

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

# For sending email
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'


# Redis Configuration
REDIS_HOST = 'localhost'
REDIS_PORT = 6379
REDIS_DB = 0

Nginx congiration

server {
    listen 80;
    server_name 192.168.1.44;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/djangoadmin/pyapps/kiji_project/;
    }

    location /media/ {
        root /home/djangoadmin/pyapps/kiji_project/;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

Gunicorn's confiration

[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
User=djangoadmin
Group=www-data
WorkingDirectory=/home/djangoadmin/pyapps/kiji_project
ExecStart=/home/djangoadmin/pyapps/venv/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          kiji.wsgi:application

[Install]
WantedBy=multi-user.target

models.py file for videos

from django.db import models
from django.conf import settings
from django.utils import timezone
from django.contrib.auth.models import User
from teacher.models import Teacher
from django.urls import reverse

class Post(models.Model):
    teacher = models.ForeignKey(Teacher, on_delete = models.DO_NOTHING, 
                                blank=True, null=True,
                                related_name='blog_teachers')
    author = models.ForeignKey(User, on_delete = models.CASCADE,
                                related_name='blog_posts')
    title = models.CharField(max_length = 100)    

    clip = models.FileField(upload_to = 'videos/%Y/%m/%d/')

    description = models.TextField(max_length = 2400, blank = True)
    publish = models.DateTimeField(default = timezone.now)
    created = models.DateTimeField(auto_now_add = True)
    updated = models.DateTimeField(auto_now = True)


    class Meta:
        ordering = ('-publish',)

    def __str__(self):
        return self.title


    def get_absolute_url(self):
        return reverse('post-detail', args = [self.id])

models.py file for default image

from django.db import models
from django.conf import settings
from PIL import Image


class Profile(models.Model):    
    name = models.CharField(max_length = 30)    
    present_address = models.CharField(max_length = 200)
    permanent_address = models.CharField(max_length = 200)
    mobile_number = models.CharField(max_length = 14, unique = True, null=True)    
    profile_picture = models.ImageField(default = 'default_profile.jpg', upload_to='users/%Y/%m/%d/')

    def __str__(self):
        return f'{self.user.username} Profile'


    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

        img = Image.open(self.profile_picture.path)

        if img.height > 300 or img.width > 300:
            output_size = (300, 300)
            img.thumbnail(output_size)
            img.save(self.profile_picture.path)
Coduser
  • 301
  • 1
  • 2
  • 13

0 Answers0