我用视频文件创建了Django应用程序。我在用Gunicorn和Nginx。运行django服务器的用户(而不是root用户)。
我限制了视图,因此只有具有正确权限的用户才能查看这些视图。这个很好用。
但是当我看视频的时候,我可以得到网址。(./media/录象/视频.mp4)

现在每个拥有这个文件位置的人都能看到视频。
我现在有点迷路了。我需要使用像django-sendfile2这样的包吗?还是需要更改服务器设置并限制对媒体文件夹的访问?
发布于 2022-06-05 18:07:21
我确实设法使用django-私人仓库包保护了我的文件。django-sendfile2也可能有效,但我确实发现这些文档不太具有解释性。按照说明,我安装了这些软件包:
pip install django-private-storage并将以下行添加到Settings.py文件中:
INSTALLED_APPS += (
'private_storage',
)
PRIVATE_STORAGE_ROOT = os.path.join(BASE_DIR, 'private-media')在模型中,我添加了字段:
video = PrivateFileField(upload_to='videos/%Y/%m/%d', null=True)在我看来,我补充说:
from private_storage.views import PrivateStorageDetailView
class PlayJobVideo(PrivateStorageDetailView):
model = Video
model_file_field = "video"
template_name = "index/play_video.html"
def get_object(self):
return Video.objects.get(id=self.kwargs['video_id'])
def can_access_file(self, private_file):
return True然后我写了一个装饰器owner_only()
def owner-only():
def decorators(view_func, *args, **kwargs):
def wrapper_function(request, *args, **kwargs):
try:
if request.user:
if video.user == request.user:
return view_func(request, *args, **kwargs)
else:
return redirect('index:home')
except:
return redirect('index:home')
return wrapper_function
return decorators我把这个装潢师添加到了视图中。
@method_decorator(owner_only(), name='dispatch')
class PlayVideo(PrivateStorageDetailView):
...https://stackoverflow.com/questions/72499165
复制相似问题