test-farid/album/views.py

53 lines
1.8 KiB
Python

from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.core import exceptions
from django.core.exceptions import PermissionDenied
from django.http import (HttpResponse, HttpResponseBadRequest,
HttpResponseRedirect)
from django.shortcuts import render
from django.views.generic import CreateView, TemplateView
from .forms import AlbumForm
from .models import Album
from .serializers import AlbumCreateUpdateSerializer
# Create your views here.
class HomePageView(TemplateView):
template_name = 'album/homepage.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# NOTE: This will be used to update the active window in navbar
context.update({
'contact_us_create': 'active',
'albums': Album.objects.filter(is_verified=True).only('id', 'image', 'is_verified')
})
return context
def post(self, request, *args, **kwargs) -> HttpResponse:
print(request.POST.get('surname'))
form = AlbumForm(request.POST, request.FILES)
# print(form.__dict__)
if form.is_valid():
# print('opno')
instance = form.save()
messages.success(self.request, 'Image uploaded successfully.')
return HttpResponseRedirect('/')
else:
return HttpResponseBadRequest()
# raise exceptions.ValidationError(form.errors)
return
data = {'image': request.FILES.get('image')}
serializer = AlbumCreateUpdateSerializer(data=data)
serializer.is_valid(raise_exception=True)
instance = serializer.save()
messages.success(self.request, 'Image uploaded successfully.')
return HttpResponseRedirect('/')