0% found this document useful (0 votes)
2 views

Sdf

The document provides an overview of Django's generic views, including ListView, DetailView, CreateView, UpdateView, and DeleteView, highlighting their flexibility, efficiency, and consistency. It also covers extending generic views through overriding attributes and methods, using mixins, and customizing templates. Additionally, it discusses creating syndication feeds and sitemaps for Django applications.

Uploaded by

Gowdru Hudga0127
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Sdf

The document provides an overview of Django's generic views, including ListView, DetailView, CreateView, UpdateView, and DeleteView, highlighting their flexibility, efficiency, and consistency. It also covers extending generic views through overriding attributes and methods, using mixins, and customizing templates. Additionally, it discusses creating syndication feeds and sitemaps for Django applications.

Uploaded by

Gowdru Hudga0127
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

GENERIC VIEWS

Generic views in Django are pre-built, reusable views

Flexibility: Can be easily customized to fit specific requirements.

Efficiency: Reduces the amount of boilerplate code needed for common tasks.

Consistency: Provides a standard, predictable way of handling common web pa erns.

List view : ListView is a generic view used to display a list of objects from a par cular model.

Detail view : ListView is a generic view used to display a de als of single objects from a par cular
model.

Create View : CreateView is a generic view used to handle the crea on of a new object.

Update View : UpdateView is a generic view used to handle the upda ng of an exis ng object.

Delete view : DeleteView is a generic view used to handle the dele on of an object.

 Defining the views

from Django.views.generic import ListView, Detailview, CreateView, UpdateView, DeleteView


from .models import models

class MyListView(ListView):
model = Project
template_name = 'myapp/MyListView.html'

class MyDetailView(DetailView):
model = Project
template_name = 'myapp/MyDetailView.html'

class MyCreateView(CreateView):
model = Project
template_name = 'myapp/MyCreateView.html'

class MyUpdateView(UpdateView):
model = Project
template_name = 'myapp/MyUpdateView.html'
class MyDeleteView(DeleteView):
model = Project
template_name = 'myapp/MyDeleteView.html'

 url configura on

from djnago.url import path

from .views import MyListView, MyDetailView, MyCreateView, MyUpdateView, MyDeleteView

urlpa erns = [

path('ListView/',MyListView_as.view()),

path('ListView/',MyDetailView_as.view()),

path('ListView/',MyCreateView_as.view()),

path('ListView/',MyUpdateView_as.view()),

path('ListView/',MyDeleteView_as.view()),

]
EXTENDING GENERIC VIEWS

 Overriding A ributes

from django.views.generic import ListView

from .models import Student

class StudentListView(ListView):

model = Student

template_name = 'student/student_list.html'

context_object_name = 'students'

 Overriding Methods

from django.views.generic import ListView

from .models import Student

class StudentListView(ListView):

model = Student

template_name = 'students/ac ve_student_list.html'

def get_queryset(self):

return Student.objects.filter(is_ac ve=True)

 Using Mixins

from django.contrib.auth.mixins import LoginRequiredMixin

from django.views.generic import DetailView

from .models import Student


class StudentDetailView(LoginRequiredMixin, DetailView):

model = Student

template_name = 'students/student_detail.html'

login_url = '/login/'

 customizing the templates

from django.views.generic import ListView

from .models import Student

class StudentListView(ListView):

model = Student

template_name = ' student /student_list.html'


STUDENT LISTVIEW AND DETAILVIEW PROGRAM

 models.py

from django.db import models

class Student(models.Model):

first_name = models.CharField(max_length=100)

last_name = models.CharField(max_length=100)

enrollment_date = models.DateField()

def __str__(self):

return f"{self.first_name} {self.last_name}"

 views.py

from django.views.generic import ListView, DetailView

from .models import Student

class MyListView(ListView):

model = Student

template_name = 'dist.html'

class MyDetailView(DetailView):

model = Student

template_name = 'detail.html'
 url.py

from django.urls import path

from .views import MyListView, MyDetailView

urlpa erns = [

path('students/', MyListView.as_view()),

path('students/<int:pk>/', MyDetailView.as_view()),

 list.html

<html>

<body>

<h1>Student List</h1>

<ul>

{% for student in object_list %}

<li>

<a href="{% url 'detail' student.pk %}">{{ student.first_name }} {{ student.last_name }}</a></li>

{% endfor %}

</ul>

</body>

</html>

 detail.html

<html>

<body>

<h1>{{ object.first_name }} {{ object.last_name }}</h1>

<p>Enrollment Date: {{ object.enrollment_date }}</p>


<a href="{% url 'list' %}">Back to List</a>

</body>

</html>
SYNDICATION FEED

Django’s syndica on framework allows you to create feeds that can be consumed by RSS or Atom
readers.

The framework handles the genera on of the XML for the feed, so you don’t need to deal with XML
genera on manually.

 Define Your Model :

models.py :

from django.db import models

class Post(models.Model):

tle = models.CharField(max_length=200)

content = models.TextField()

pub_date = models.DateTimeField('date published')

def __str__(self):

return self. tle

 Create a Feed Class

feeds.py

from django.contrib.syndica on.views import Feed

from .models import Post

class PostsFeed(Feed):

tle = "Latest Blog Posts"


link = "/feeds/posts/"

descrip on = "Updates on the latest blog posts."

def items(self):

return Post.objects.order_by('-pub_date')[:5]

def item_ tle(self, item):

return item. tle

def item_descrip on(self, item):

return item.content

def item_link(self, item):

return f"/posts/{item.pk}/"

 Configure URLs

from django.urls import path

from .feeds import PostsFeed

urlpa erns = [

path('feeds/posts/', PostsFeed()),

tle : The tle of the feed.

link : The URL for accessing the feed.

descrip on : A brief descrip on of the feed.

items() : Specifies which items to include in the feed.

item_ tle() : Provides the tle for each feed item.

item_descrip on() : Provides the descrip on/content for each feed item.

item_link() : Provides a link to each feed item.


SITEMAP CLASS

from django.contrib.sitemaps import Sitemap

from .models import Post

class PostSitemap(Sitemap):

def items(self):

return Post.objects.all()

def lastmod(self, obj):

return obj.updated_at

def changefreq(self, obj):

return 'weekly'

def priority(self, obj):

return 0.7

You might also like