Breaking News

New Updates

Movie App

Create project and is same as travel app
Go to movie app name as "MovieHub" --> models.py --> Enter below;
from django.contrib import admin
from . models import Movie
# Register your models here.
import moviehub

admin.site.register(Movie)


in modes any changes should do
python manage.py makemigrations
python manage.py migrate


Create cuper user
ptython manage.py createsuperuser 
user name: admin 
appword: ad123min456
appword (again): ad123min456


note:13.5
onnum kannan pattillarunnu ippam kanam
go to "MovieHub" --> admin.py
from django.contrib import admin
from . models import Movie
# Register your models here.
import moviehub

admin.site.register(Movie)


go to "MovieHub" -->views.py for (checking)
from django.http import HttpResponse, HttpRequest
from django.shortcuts import render

# Create your views here.
#vritua lenv : movie
from moviehub.models import Movie


def index(request):
    movie=Movie.objects.all()
    context = {
        'movie_list': movie
    }
    return render(request, 'index.html', context)


go to "MovieHub" -->views.py (yes, its work properly, edete last line)
from django.http import HttpResponse, HttpRequest
from django.shortcuts import render

# Create your views here.
#vritua lenv : movie
from moviehub.models import Movie


def index(request):
    movie=Movie.objects.all()
    context = {
        'movie_list': movie
    }
    return HttpRespose("New update", context)


create new folder Template, template and indux page.



Name space Go to app Urls enter below:
app_name = 'MUV'


Go to Templates index page.
{% for i in movie_list %}
<a href="{% url 'MUVi:detail' i.id %}">{{i.name}}</a><br>


Load Static file


Go to Project Settings Static urls:
  STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
  STATIC_ROOT = os.path.join(BASE_DIR, 'assets')
  MEDIA_URL = '/media/'
  MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
  


Go to Project Urls Static urls:
if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL,
                          document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)
  import:
    settings : Django-conf-setiings
    static : django.conf.urls.static
    


Create a Static folder from  create new style.css file
Base ആയിട്ടുള്ള ഹെഡർ, ഉം ബോട്ടവും, add ചെയ്തതിനുശേഷം Body വേണ്ടിടത്ത് ഇങ്ങനെ ചെയ്യാം :
{% extends 'base.html' %}

<!--body content start -->

{% for i in movie_list %}
<a href="{% url 'MUV:detail' i.id %}">{{i.name}}</a><br>

<!--body content end -->

{% endfor %}


Base
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome to Moviehub</title>
    <link rel="stylesheet" href="{% static style.css %}">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<!--navigation bar start -->
<nav class="navbar navbar-dark bg-dark">
  <div class="container">
    <a class="navbar-brand" href="#">
      <img src="https://getbootstrap.com/docs/5.1/assets/brand/bootstrap-logo.svg" alt="" width="30" height="24">Movie Hub
    </a>
  </div>
</nav>
<!--navigation bar end -->
<!--body start -->
{% block body %}
{% endblock %}
<!--body End -->
</body>
</html>


Index page
{% extends 'base.html' %}
{% block body %}
<!--body content start -->
{% for i in movie_list %}
<a href="{% url 'MUV:detail' i.id %}">{{i.name}}</a><br>
<!--body content end -->
{% endfor %}
{% endblock %}


ഇത് Detail page ൽ പോകാനാണ് eg: Drishyam enna Name il click cheyyumbol, Puthiya page open ayi thazhe kanunna pole ulla vivarangal varum.
{% extends 'base.html' %}
{% block body %}
{{movie.id}}<br>
{{movie.name}}<br>
{{movie.desc}}<br>
{{movie.year}}<br>
{% endblock %}


Adding image on Datafile movieapp
GO to models.py  add image field.
# Create your models here.
class Movie(models.Model):
    objects = None
    name=models.CharField(max_length=25)
    desc=models.TextField()
    year=models.IntegerField()
    img=models.ImageField(upload_to='gallery') #this is newly add line.
  ൽ  എന്തു മാറ്റങ്ങൾ വരുത്തിയാലും makemigrations, and migrate ഉം കൊടുക്കണം,
Warning :
Nerathe kodutha, movie name, desc,  and year. Ithente koode puthiya oru field aya  Image koduthathukond, ippol athinullil img unnum illa thukond enthengilum ippol thane kodukkaan parayum.
Appol Option 2, koduthathinu shesham, single quates il enthengilum ‘jfhdfd’ kodukkuka,
Athappol ingane kanikkum.
Adminpage
Admin page image adding screanshort.


Adding page form.
{% extends 'base.html' %}
{% block body %}
<title>Add new movie</title>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
    <input type="text" name="name" placeholder="Enter the movie name"><br>
    <input type="text" name="desc" placeholder="Enter the note of the movie"><br>
    <input type="text" name="year" placeholder="Enter the movie releasing Year"><br>
    <input type="file" name="img" placeholder="Enter the movie releasing Year"><br>
    <input type="submit">
{% endblock %}
</form>


All file Full

Base
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Welcome to Moviehub</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    <link rel="stylesheet" href="{% static 'style.css' %}">
</head>
<body>
<!--navigation bar start -->
<nav class="navbar navbar-dark bg-dark">
    <a class="navbar-brand" href="#">
      Movie Hub
    </a>
</nav>
{% block body %}
{% endblock %}
<!--body End -->
</body>
</html>


Detail page
{% extends 'base.html' %}
{% block body %}
<img src="{{movie.img.url}}" width="100" height="150">
{{movie.id}}<br>
{{movie.name}}<br>
{{movie.desc}}<br>
{{movie.year}}<br>
{% endblock %}


Index page il
{% extends 'base.html' %}
{% block body %}
<!--body content start -->
{% for i in movie_list %}
<img src="{{i.img.url}}" width="200" height="225">
<a href="{% url 'MUV:detail' i.id %}">{{i.name}}</a><br>
<!--body content end -->
{% endfor %}
{% endblock %}


  Important:
കുറെ ദിവസമായി ഇരിക്കുന്നു, Add  Page ൽ. 
Movie name, Desc, Year and Phototo upload ചെയ്തിട്ട് index page ൽ വരുന്നില്ലരുന്നു, 
കാരണം:
Admin page വഴി add ചെയ്ത movies details delete ചെയ്തിട്ട് വേണമാ രുന്നു, add page, വഴി movies details ൽ കൊടുക്കാനും അതു main pageൽ വരാനും.  

    


Edit Form

ചെയ്ത movie details നെ Edit / Update ചെയ്യാൻ ഉപയൊഗിക്കുന്നു. Go to App folder  Create a new python file  name: forms.py
from django import forms
from .models import Movie

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


Go to App folder  Urls.py  Enter below,
path('update/<int:id>/', views.update, name='update')


Go to App folder  Views.py  Enter below;
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})


Go to templates  Create new folder name as; edite.html
{% extends 'base.html' %}
{% block body %}
<form method="POST" enctype="multipart/form-data">
    {% csrf_token %}
    {{form.as_p}}
    <input type="submit">
</form>
{% endblock %}


Add button on detail page to update/edite movies.
Go to template  detail.
{% extends 'base.html' %}
{% block body %}
<img src="{{movie.img.url}}" width="100" height="150">
{{movie.id}}<br>
{{movie.name}}<br>
{{movie.desc}}<br>
{{movie.year}}<br>
<a href="{% url 'MUV:update' movie.id %}">update</a> 
{% endblock %}


Delete Page

Got to app  urls.py
from django.contrib import admin
from django.urls import path, include

#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') #This is newly added
]


Got to app  view.py page
def delete(request, id):
    if request.method=='POST':
        movie=Movie.objects.get(id=id)
        movie.delete()
        return redirect('/')
    return render(request, 'delete.html')


Got to template folder,  create new file ‘delete.html’
<title>To delete </title>
<form method="POST" enctype="multipart/form-data">
  {% csrf_token %}
  <h2>Are you want to delete. ?</h2>
  <input type="submit">
</form>


Got to template folder,  ‘detail.html’ and add “Delete” pages on retail page.
{% extends 'base.html' %}
{% block body %}
<img src="{{movie.img.url}}" width="100" height="150">
{{movie.id}}<br>
{{movie.name}}<br>
{{movie.desc}}<br>
{{movie.year}}<br>
<a class="btn btn-warning" href="{% url 'MUV:update' movie.id %}">Update</a>
<a class="btn btn-danger" href="{% url 'MUV:delete' movie.id%}">Delete</a>
{% endblock %}


Add Beautiful theme using Boostrap.

Got to template folder.
{% extends 'base.html' %}
{% block body %}


<div class="container">
    <div class="row">
        {% for i in movie_list %}
        <div class="col-md-3">
            <!--start-->
            <div class="card" style="width: 18rem;">
              <img src="{{i.img.url}}" class="card-img-top" alt="{{i.name}}" width="150" height="300">
              <div class="card-body">
                <h5 class="card-title">{{i.name}}</h5>
                <p class="card-text">{{i.desc}}</p>
                <a href="{% url 'MUV:detail' i.id %}" class="btn btn-primary">{{i.name}}</a>
              </div>
            </div>
            <!--end-->
        </div>
        {% endfor %}
    </div>
</div>
{% endblock %}


Detail page modifying using boostrap
{% extends 'base.html' %}
{% block body %}

<div class="container">
    <div class="row">
        <div class="col-md-6">
            <img src="{{movie.img.url}}" width="300" height="450">
        </div>
        <div class="col-md-6">

            <table class="table table-striped">
                <thead>
                    <tr>
                      <th scope="col">Movie name:</th>
                      <th scope="col">{{movie.name}}</th>
                    </tr>
                  </thead>
                  <tbody>
                    <tr>
                      <th scope="row">Release date:</th>
                      <td>{{movie.year}}</td>
                    </tr>
                    <tr>
                      <th scope="row">Description:</th>
                      <td>{{movie.desc}}</td>

                    </tr>
                    <tr>
                      <th scope="row">Movie App id:</th>
                      <td colspan="2">{{movie.id}}</td>
                    </tr>
                  </tbody>
            </table>
            <a class="btn btn-warning" href="{% url 'MUV:update' movie.id %}">Update</a>
            <a class="btn btn-danger" href="{% url 'MUV:delete' movie.id%}">Delete</a>
        </div>

    </div>

</div>
{% endblock %}

All above from function base views to Class based views

All class are different uses.

class Home(ListView):
    model = Movie
    template_name = 'home.html'
    context_object_name = 'movielist'

class Plus(CreateView):
    model = Movie
    template_name = 'plus.html'
    fields = ['name', 'year', 'directer', 'stars']
    success_url = reverse_lazy("movies:home")

class Info(DetailView):
    model = Movie
    template_name = 'info.html'
    context_object_name = 'movie'

class Erase(DeleteView):
    model = Movie
    template_name = 'erase.html'
    context_object_name = 'task'
    success_url = reverse_lazy("movies:home")

class Update(UpdateView):
    model = Movie
    template_name = 'update.html'
    context_object_name = 'movie'
    fields = ('name', 'year', 'directer', 'stars')

    def get_success_url(self):
        return reverse_lazy('movies:info', kwargs={'pk':self.object.id})

All above Class urls pages

All class are different uses.

from django.urls import path, include
from . import views

app_name= 'movies'
urlpatterns = [
    path('home/', views.Home.as_view(), name='home'),
    path('<int:pk>/info/', views.Info.as_view(), name='info'),
    path('<int:pk>/erase/', views.Erase.as_view(), name='erase'),
    path('<int:pk>/update/',views.Update.as_view(), name='update'),
    path('plus/', views.Plus.as_view(), name='plus'),

]

All above from function base views to Class based views

All class are different uses.

No comments