DEV Community

Cover image for 10 Django Built-in Views You Need to Know for Your Next Project
Abdulla Fajal
Abdulla Fajal

Posted on

10 Django Built-in Views You Need to Know for Your Next Project

Django is a powerful web framework for building web applications quickly and efficiently. It includes many built-in views that can be used to handle common tasks such as displaying a list of objects, creating new objects, and editing existing objects. In this answer, I will cover 10 Django built-in views that you should know for your next project, along with proper explanations and examples.

ListView

ListView The ListView is a built-in view that displays a list of objects from a model. It is commonly used to display a list of blog posts, products, or other types of content. Here's an example:

from django.views.generic import ListView
from .models import Post

class PostListView(ListView):
    model = Post
    template_name = 'blog/post_list.html'
    context_object_name = 'posts'
    ordering = ['-date_posted']
Enter fullscreen mode Exit fullscreen mode

In this example, the ListView is used to display a list of blog posts from the Post model. The template_name attribute specifies the template to use for rendering the view, and the context_object_name attribute specifies the name of the context variable that will contain the list of objects.

DetailView

DetailView The DetailView is a built-in view that displays the details of a single object from a model. It is commonly used to display a blog post, product details, or other types of content. Here's an example:

from django.views.generic import DetailView
from .models import Post

class PostDetailView(DetailView):
    model = Post
    template_name = 'blog/post_detail.html'
    context_object_name = 'post'
Enter fullscreen mode Exit fullscreen mode

In this example, DetailView is used to display the details of a single blog post from the Post model. The template_name attribute specifies the template to use for rendering the view, and the context_object_name attribute specifies the name of the context variable that will contain the object.

CreateView

CreateView The CreateView is a built-in view that displays a form to create a new object from a model. It is commonly used to create new blog posts, products, or other types of content. Here's an example:

from django.views.generic import CreateView
from .models import Post
from .forms import PostForm

class PostCreateView(CreateView):
    model = Post
    form_class = PostForm
    template_name = 'blog/post_form.html'
Enter fullscreen mode Exit fullscreen mode

In this example, the CreateView is used to display a form to create a new blog post from the Post model. The form_class attribute specifies the form to use for rendering the form, and the template_name attribute specifies the template to use for rendering the view.

UpdateView

UpdateView The UpdateView is a built-in view that displays a form to edit an existing object from a model. It is commonly used to edit blog posts, products, or other types of content. Here's an example:

from django.views.generic import UpdateView
from .models import Post
from .forms import PostForm

class PostUpdateView(UpdateView):
    model = Post
    form_class = PostForm
    template_name = 'blog/post_form.html'

Enter fullscreen mode Exit fullscreen mode

In this example, the UpdateView is used to display a form to edit an existing blog post from the Post model. The form_class attribute specifies the form to use for rendering the form, and the template_name attribute specifies the template to use for rendering the view.

DeleteView

DeleteView The DeleteView is a built-in view that displays a confirmation page before deleting an object from a model. It is commonly used to delete blog posts, products, or other types of content. Here's an example:

from django.views.generic import DeleteView
from .models import Post
from django.urls import reverse_lazy

class PostDeleteView(DeleteView):
    model = Post
    template_name = 'blog/post_confirm_delete.html'
    success_url = reverse_lazy('blog-home')
Enter fullscreen mode Exit fullscreen mode

In this example, the DeleteView is used to display a confirmation page before deleting a blog post from the Post model. The template_name attribute specifies the template to use for rendering the view, and the success_url attribute specifies the URL to redirect to after the object is deleted.

FormView

FormView The FormView is a built-in view that displays a form and processes user input. It can be used to implement custom forms or handle form submissions. Here's an example:

from django.views.generic import FormView
from .forms import ContactForm
from django.urls import reverse_lazy

class ContactView(FormView):
    form_class = ContactForm
    template_name = 'contact.html'
    success_url = reverse_lazy('contact-success')

    def form_valid(self, form):
        form.send_email()
        return super().form_valid(form)
Enter fullscreen mode Exit fullscreen mode

In this example, the FormView is used to display a contact form and send an email when the form is submitted. The form_class attribute specifies the form to use for rendering the form, and the template_name attribute specifies the template to use for rendering the view. The success_url attribute specifies the URL to redirect to after the form is successfully submitted. The form_valid() method is overridden to send the email when the form is submitted.

For Read More Click Here Espere.in

Top comments (0)