DEV Community

Cover image for Writing Views in Django

Posted on

Writing Views in Django

In Django, a view is a Python function that takes a web request and returns a web response. The response can be in the form of HTML, JSON, XML, Image or anything at all. Yes anything

Below is a simple view that displays a message to the user

from django.http import HttpResponse

def hello(request):
    return HttpResponse('Hello, World!')
Enter fullscreen mode Exit fullscreen mode

The code above is simply returning an HTTP response, but rendering templates is quite similar and easy.

Views are an important part of any Django application because they handle the logic of your application. They are responsible for processing requests, interacting with models to retrieve data, and returning a response to the client.

Don't stress too much about models read my next article, I will write about it

To create a view in Django, you will need to define a function in your app's file and map it to a URL pattern. For example, let's say you want to create a view that displays a list of blog posts. You could define a view like this:

from django.shortcuts import render
from .models import BlogPost

def list_posts(request):
    posts = BlogPost.objects.all()
    return render(request, 'blog/list_posts.html', {'posts': posts})
Enter fullscreen mode Exit fullscreen mode

This view uses the Django ORM to retrieve all blog posts from the database and passes them to a template to be rendered.

To map this view to a URL, you will need to create a file called in your app directory and define a URL pattern then import views into the file. For example:

from django.urls import path
from . import views

urlpatterns = [
    path('posts/', views.list_posts, name='list_posts'),
Enter fullscreen mode Exit fullscreen mode

This will create a URL pattern that maps the /posts/ URL to the list_posts view.

In addition to simple views like the one shown above, it is important to note that Django also supports class-based views, which are classes that inherit from the View class and implement one or more methods to handle different types of requests. For example, you could create a class-based view for the list_posts view like this:

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

class ListPostsView(ListView):
    model = BlogPost
    template_name = 'blog/list_posts.html'
    context_object_name = 'posts'
Enter fullscreen mode Exit fullscreen mode

This view will handle the same functionality as the function-based view, but it is organized as a class with separate methods for handling different types of requests (e.g. get, post, put, etc.).

Simple Definition of Django ORM

Django ORM: Django Object Relational Mapper is like a helper that makes it easy for people (programmers) to talk to a computer program (database) and ask it to remember things like a list, but instead of writing a lot of confusing codes, they use simple words that the helper (ORM) understand and then it translates it to the computer language that it can understand.


There are many more advanced concepts you can explore with Django views, such as decorators, generic views, and view mixins. There is no point jumping into this now, we can always learn along the way.

For broader information about Django views, you can refer to the documentation.

Top comments (0)