Advertisement
MadCortez

views.py

May 16th, 2023
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. from django.shortcuts import render, redirect
  2. from django.views import View
  3. from .models import Movie
  4.  
  5. class MovieListView(View):
  6.     def get(self, request):
  7.         movies = Movie.objects.all()
  8.         return render(request, 'movies/movie_list.html', {'movies': movies})
  9.  
  10. class MovieCreateView(View):
  11.     def get(self, request):
  12.         return render(request, 'movies/movie_create.html')
  13.    
  14.     def post(self, request):
  15.         title = request.POST.get('title')
  16.         director = request.POST.get('director')
  17.         release_date = request.POST.get('release_date')
  18.        
  19.         movie = Movie(title=title, director=director, release_date=release_date)
  20.         movie.save()
  21.        
  22.         return redirect('movie_list')
  23.  
  24. class MovieDeleteView(View):
  25.     def post(self, request, movie_id):
  26.         movie = Movie.objects.get(id=movie_id)
  27.         movie.delete()
  28.         return redirect('movie_list')
  29.  
  30. class MovieUpdateView(View):
  31.     def get(self, request, movie_id):
  32.         movie = Movie.objects.get(id=movie_id)
  33.         return render(request, 'movies/movie_update.html', {'movie': movie})
  34.  
  35.     def post(self, request, movie_id):
  36.         movie = Movie.objects.get(id=movie_id)
  37.         movie.title = request.POST['title']
  38.         movie.director = request.POST['director']
  39.         movie.release_date = request.POST['release_date']
  40.         movie.save()
  41.         return redirect('movie_list')
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement