Breaking News

New Updates

moviehub 2024 - Django Movie Review App

Django Movie Review App

  1. Creat virtualenv (option 1) [docs]

  2. python -m virtualenv venv
    venv\Scripts\activate
    or
    venv\Scripts\activat.bat 
    
  3. Creat virtualenv (option 2) [docs]

  4. python -m vritualenv venv
    cd venv
    cd Scripts
    cd activate
    

Django Movie Review App

Python Django Full Course for Beginners+react
  1. on project/settings.py :

  2. pip install django
    django-admin starproject moviepro
    cd mysite
    python manage.py startapp movieapp
    
  3. on project/settings.py : [doc



  4. on project/urls.py :

  5. from django.conf import settings
    from django.conf.urls.static import static
    from django.contrib import admin
    from django.urls import path, include
    
    import moviehub
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', include("moviehub.urls"))
    ]
    if settings.DEBUG:
        urlpatterns += static(settings.STATIC_URL,
                              document_root=settings.STATIC_ROOT)
        urlpatterns += static(settings.MEDIA_URL,
                              document_root=settings.MEDIA_ROOT)
    


  6. on app/urls.py

  7. from django.contrib import admin
    from django.urls import path, include
    from django.views.generic import TemplateView
    
    #name space is MUV
    app_name = 'MUV'
    import moviehub
    from . import views
    
    urlpatterns = [
        path('', views.index, name='index'),
        path('movie/<int:movie_id>/', views.detail, name='detail'),
        path('add/', views.add_movie, name='add_movie'),
        path('update/<int:id>/', views.update, name='update'),
        path('delete/<int:id>/', views.delete, name='delete')
    ]
    


  8. on app/models.py:

  9. from django.db import models
    from django.urls import reverse
    from django.utils import timezone
    from datetime import timedelta
    
    
    # Create your models here.
    class Movie(models.Model):
        # category=models.ForeignKey(Actor, on_delete=models.CASCADE)
        name=models.CharField(max_length=25)
        desc=models.TextField(db_index=True)
        year=models.IntegerField()
        img=models.ImageField(upload_to='gallery')
    
        # class Meta:
        #     ordering=('-name',)
    
        def __str__(self):
            return self.name
        # def get_absolute_url(self):
        #     return reverse('MUV:index', args=[self.id,])
    


  10. on app/views.py :

  11. from django.http import HttpResponse, HttpRequest
    from django.shortcuts import render, redirect
    from django.shortcuts import get_object_or_404
    from django.contrib import messages
    from moviehub.forms import MovieForm
    from moviehub.models import Movie
    
    
    
    # from moviehub.models import Actor
    
    # Create your views here.
    # ------------------------------------
            #vritua lenv : movie
            # username: surya
            # email: surya@gmail.com
            # pasword: admin@123
    # ------------------------------------
    
    def index(request):
        movie=Movie.objects.all()
        context = {
            'movie_list': movie
        }
        return render(request, 'index.html', context)
    
    
    
    
    def detail(request, movie_id):
        movie=Movie.objects.get(id=movie_id)
    
        # forse delete witho confirmation.
        # if request.method=='POST':
        #     movie=Movie.objects.get(id=movie_id)
        #     movie.delete()
        #     messages.success(request, 'Movie deleted successfully !')
        #     return redirect('/')
    
        return render(request, "detail.html", {'movie': movie})
    
    
    def add_movie(request):
        if request.method == "POST":
            name = request.POST.get('name')
            desc = request.POST.get('desc')
            year = request.POST.get('year')
            img = request.FILES['img']
            movie = Movie(name=name, desc=desc, year=year, img=img)
            movie.save()
            return redirect("/")
        return render(request, 'add.html')
    
    
    def update(request, id):
        movie=Movie.objects.get(id=id)
        form=MovieForm(request.POST or None, request.FILES, instance=movie)
        if form.is_valid():
            form.save()
            return redirect('/')
        return render(request, 'edit.html', {'form':form, 'movie':movie})
    
    # def delete(request, id):
    #     movie=Movie.objects.get(id=id)
    #     if request.method=='POST':
    #         movie=Movie.objects.get(id=id)
    #         movie.delete()
    #         messages.success(request, f'The movie "{movie.name}" deleted successfully !')
    #         return redirect('/')
    #     return render(request, 'delete.html', {'movie':movie})
    
    
    def delete(request, id):
        # Fetch the movie object or return a 404 error if not found
        movie = get_object_or_404(Movie, id=id)
        movie_name = movie.name  # Capture the movie name before deletion
    
        if request.method == 'POST':
            movie.delete()
            messages.success(request, f'The movie {movie_name} was deleted successfully')
            return redirect('/')  # Redirect after deletion
    
        return render(request, 'delete.html', {'movie': movie})
    


  12. on app/forms.py

  13. from django import forms
    from .models import Movie
    
    
    class MovieForm(forms.ModelForm):
        class Meta:
            model=Movie
            fields=['name', 'desc', 'year', 'img']
    

No comments