Class Based View
< h1> Class based Views
< h2>List View
Class based generic views.py
Goto todo app --> view.py
Goto todo app urls.py
< h2>Detail view Class based generic views.py
1. Goto todo app view.py
2. Goto todo app urls.py
important pk (primary key)
3. go to temlapte details.htnl
4. To access the must be enter the id on link, eg: Title http://127.0.0.1:8000/cbvdetail/69/ < h2>Update View Class based generic views.py
1. Goto todo app view.py
2. Go to todoapp urls.py
3. Goto Templates updates.html
4. Link for Title http://127.0.0.1:8000/cbvupdate/69/e < h2>Delete View 1. Goto todo app view.py
2. Goto todo app urls..py
3. Delete page same as the previos delet.html
4. Ling for delete: Title http://127.0.0.1:8000/cbvdelete/69/
Goto todo app --> view.py
from django.views.generic import ListView class TaskListview(ListView): model = Task template_name = 'home.html' context_object_name = 'task'
Goto todo app urls.py
from . import views from django.urls import path, include urlpatterns = [ path('', views.note, name='note'), path('delete/<int:taskid>/', views.delete, name='delete'), path('update/<int:id>/', views.update, name='update'), path('cbvhome/', views.TaskListview.as_view(), name='cbvhome'), ]
< h2>Detail view Class based generic views.py
1. Goto todo app view.py
from django.views.generic.detail import DetailView # Class Based DetailView class TaskDetailview(DetailView): model = Task template_name = 'details.html' context_object_name = 'i'
2. Goto todo app urls.py
path('cbvdetail/<int:pk>/', views.TaskDetailview.as_view(), name='cbvdetail')
3. go to temlapte details.htnl
{% extends 'base.html' %} {% block content %} <div class="card border-warning mb-6 shadow" style="max-width: 35rem;"> <div class="card-header">{{i.date}}</div> <div class="card-body"> <h5 class="card-title">{{i.name}}.</h5> <p class="card-text">{{i.priority}}.</p> <a class="btn btn-danger" href="{% url 'delete' i.id %}">Done</a> <a class="btn btn-warning" href="{% url 'update' i.id %}">Update</a> </div> </div><br> {% endblock %}
4. To access the must be enter the id on link, eg: Title http://127.0.0.1:8000/cbvdetail/69/ < h2>Update View Class based generic views.py
1. Goto todo app view.py
from django.views.generic.edit import UpdateView # Class update views class TaskUpdateview(UpdateView): model = Task template_name = 'update.html' context_object_name = 'task' fields = ('name', 'priority', 'date') def get_success_url(self): return reverse_lazy('cbvdetail', kwargs={'pk':self.object.id})
2. Go to todoapp urls.py
path('cbvupdate/<int:pk>/', views.TaskUpdateview.as_view(), name='cbvupdate')
3. Goto Templates updates.html
{% extends 'base.html' %} {% block content %} <div class="col-6"> <!--Card contents start--> <div class="card border-warning mb-6 shadow" style="max-width: 35rem;"> <div class="card-header">Task Update</div> <div class="card-body"> <form method="POST"> {% csrf_token %} <p>{{form.as_p}}</p> <input type="submit" class="btn btn-success"> </form> </div> </div> <!--Card contents end--> </div> {% endblock %}
4. Link for Title http://127.0.0.1:8000/cbvupdate/69/e < h2>Delete View 1. Goto todo app view.py
from django.views.generic.edit import UpdateView, DeleteView # Delete views class TaskDeleteView(DeleteView): model = Task template_name = 'delete.html' success_url = reverse_lazy('cbvhome')
2. Goto todo app urls..py
path('cbvdelete/<int:pk>/', views.TaskDeleteview.as_view(), name='cbvdelete')
3. Delete page same as the previos delet.html
{% extends 'base.html' %} {% block content %} <form method="POST"> {% csrf_token %} <p>Are you sure this task is Done?</p> <input type="submit" class="btn btn-success"> </form> {% endblock %}
4. Ling for delete: Title http://127.0.0.1:8000/cbvdelete/69/
No comments