我正在处理一个视频博客的Django演示项目,该项目已托管在具有Gunicorn,Nginx,Redis和Postgresql的本地Ubuntu Server 18.04 LTS上。我已经在Virtualbox上安装了该服务器,并分配了固定IP。静态文件(html,css,js)运行良好,但视频和媒体文件夹的默认图像文件未显示。静态和媒体文件夹都位于项目的根目录中。我认为尚未正确配置Nginx配置文件。但是,当我使用开发服务器检查它们时,一切正常。我非常新,亲爱的,并且缺乏高级编程知识。因此,我正在寻求您的帮助以解决此问题。如果有任何错误,请看下面的代码。预先感谢!
浏览器控制台错误
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
项目结构
settings.py文件的一部分
# 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伙伴关系
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;
}
}
独角兽的没收
[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文件
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文件
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)