<filename>videos/views.py
from django.shortcuts import redirect, render, get_object_or_404
from django.http import HttpResponse
from django.core.paginator import Paginator
from django.contrib.auth.models import User
from django.contrib import messages
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from videos.models import Video
from django.contrib.auth.decorators import login_required

# Create your views here.


@login_required(login_url="loginform")
def upload_video(request):
    if not request.user.is_superuser:
        return redirect("create_idp")
    if request.method == "POST":
        caption = request.POST['caption']
        video = request.FILES['video']
        description = request.POST['description']
        video_obj = Video(caption=caption, video=video,
                          description=description)
        video_obj.save()
        messages.success(request, "Vieo uploaded successfully")
        if request.POST['action'] == "upload":
            return redirect("videos_list")
        else:
            return redirect("upload_video")
    else:
        return render(request, template_name="videos/uploadvideo.html", context={})


@login_required(login_url="loginform")
def video_list(request):
    if not request.user.is_superuser:
        return redirect("create_idp")
    videos = Video.objects.all()
    pages = Paginator(videos, 3)
    page_number = request.GET.get('page')
    videos = pages.get_page(page_number)
    page_count = pages.page_range
    return render(request, 'videos/videoslist.html', context={
        'page': videos,
        'page_count': page_count
    })


@login_required(login_url="loginform")
def delete_video(request, id):
    if not request.user.is_superuser:
        return redirect("create_idp")
    video = get_object_or_404(Video, pk=id)
    messages.success(request, video.caption+" video deleted successfully")
    video.delete()
    return redirect("videos_list")


@login_required(login_url="loginform")
def update_video_information(request, id):
    if not request.user.is_superuser:
        return redirect("create_idp")
    video = get_object_or_404(Video, pk=id)
    if request.method == "POST":
        caption = request.POST['caption']
        description = request.POST['description']
        video.caption = caption
        video.description = description
        video.save()
        messages.success(request, "video information updated successfully")
        return redirect("videos_list")
    else:
        return render(request, template_name="videos/updatevideo.html", context={
            'video': video
        })


@login_required(login_url="loginform")
def update_video(request, id):
    if not request.user.is_superuser:
        return redirect("create_idp")
    target_video = get_object_or_404(Video, pk=id)
    if request.method == "POST":
        video = request.FILES['video']
        target_video.video = video
        target_video.save()
        messages.success(request, "video updated successfully")
        return redirect("update_video_information", id)

<filename>videos/views.py
from django.shortcuts import redirect, render, get_object_or_404
from django.http import HttpResponse
from django.core.paginator import Paginator
from django.contrib.auth.models import User
from django.contrib import messages
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from videos.models import Video
from django.contrib.auth.decorators import login_required

# Create your views here.


@login_required(login_url="loginform")
def upload_video(request):
    if not request.user.is_superuser:
        return redirect("create_idp")
    if request.method == "POST":
        caption = request.POST['caption']
        video = request.FILES['video']
        description = request.POST['description']
        video_obj = Video(caption=caption, video=video,
                          description=description)
        video_obj.save()
        messages.success(request, "Vieo uploaded successfully")
        if request.POST['action'] == "upload":
            return redirect("videos_list")
        else:
            return redirect("upload_video")
    else:
        return render(request, template_name="videos/uploadvideo.html", context={})


@login_required(login_url="loginform")
def video_list(request):
    if not request.user.is_superuser:
        return redirect("create_idp")
    videos = Video.objects.all()
    pages = Paginator(videos, 3)
    page_number = request.GET.get('page')
    videos = pages.get_page(page_number)
    page_count = pages.page_range
    return render(request, 'videos/videoslist.html', context={
        'page': videos,
        'page_count': page_count
    })


@login_required(login_url="loginform")
def delete_video(request, id):
    if not request.user.is_superuser:
        return redirect("create_idp")
    video = get_object_or_404(Video, pk=id)
    messages.success(request, video.caption+" video deleted successfully")
    video.delete()
    return redirect("videos_list")


@login_required(login_url="loginform")
def update_video_information(request, id):
    if not request.user.is_superuser:
        return redirect("create_idp")
    video = get_object_or_404(Video, pk=id)
    if request.method == "POST":
        caption = request.POST['caption']
        description = request.POST['description']
        video.caption = caption
        video.description = description
        video.save()
        messages.success(request, "video information updated successfully")
        return redirect("videos_list")
    else:
        return render(request, template_name="videos/updatevideo.html", context={
            'video': video
        })


@login_required(login_url="loginform")
def update_video(request, id):
    if not request.user.is_superuser:
        return redirect("create_idp")
    target_video = get_object_or_404(Video, pk=id)
    if request.method == "POST":
        video = request.FILES['video']
        target_video.video = video
        target_video.save()
        messages.success(request, "video updated successfully")
        return redirect("update_video_information", id)

# Create your views here.