Django Views

After creating and adding data to our models, we need to display the data to our site. Rendering data is a two-step process. First we need to create our view function and then we need to pass the data to our template. Here we will focus on the view function.

A view function is a Python function that takes a Web request and returns a Web response. This response can be the HTML contents of a Web page, or a redirect, or a 404 error, or an XML document, or an image, anything that a web browser can display. Django views are part of the user interface — they usually render the HTML/CSS/Javascript in your Template files into what you see in your browser when you render a web page.

At first, let’s create a sample view function that will simply show normal HTML content.

Python3




from django.http import HttpResponse
 
# create a function
def home(request):
     
    return HttpResponse("<h1>Welcome to w3wiki</h1>")


 
 

Let’s step through this code one line at a time:

 

  • First, we import the class HttpResponse from the django.http module, along with Python’s datetime library.
  • Next, we define a function called home. This is the view function. Each view function takes an HttpRequest object as its first parameter, which is typically named request.
  • The view returns an HttpResponse object that contains the generated response. Each view function is responsible for returning an HttpResponse object.

 

The above Function will render the text Welcome to w3wiki as h1 on the page. Now the question that may be arising is at what URL this function will be called and how will we handle such URLs. Don’t worry we will handle URL in the section but in this section let us continue with the Django views only.

 

Types of Views

 

Django views are divided into two major categories:-

 

  • Function-Based Views
  • Class-Based Views

Function-Based Views

 

Function-based views are writer using a function in python which receives as an argument HttpRequest object and returns an HttpResponse object. Function-based views are generally divided into 4 basic strategies, i.e., CRUD (Create, Retrieve, Update, Delete). CRUD is the base of any framework one is using for development. The one we created above is a function-basedClass-Based Views

 

Class-Based Views

 

Class-based views provide an alternative way to implement views as Python objects instead of functions. They do not replace function-based views, but have certain differences and advantages when compared to function-based views:

 

  • Organization of code related to specific HTTP methods (GET, POST, etc.) can be addressed by separate methods instead of conditional branching.
  • Object-oriented techniques such as mixins (multiple inheritances) can be used to factor code into reusable components. view.

Getting started with Django

Python Django is a web framework that is used to create web applications very efficiently and quickly. Django is called a battery included framework because it contains a lot of in-built features such as Django Admin Interface, default database – SQLite3, etc. Django provides various ready-made components such as a way to handle user authentication, a management panel for your website, forms, a way to upload files, etc.

In this article, we will learn Django by creating a basic blogging web application.  

Similar Reads

Why Django Framework?

Django is a rapid web development framework that can be used to develop fully fleshed web applications in a short period of time. It’s very easy to switch databases in the Django framework. It has a built-in admin interface which makes it easy to work with it. Django is a fully functional framework that requires nothing else. It has thousands of additional packages available. It is very scalable....

Structure of Django Web Application

Django is based on MVT (Model-View-Template) architecture. MVT is a software design pattern for developing a web application....

Setting the Development Environment

It is always advisable to use a virtual environment before starting any Django project because sometimes when you are working on multiple projects you might need different versions of Django. The virtual environment creates a separate environment for different projects and you can install dependencies of each project separately. To create a virtual environment type the below command in the terminal –...

Installing Django

We can install Django using the pip command. To install this type the below command in the terminal....

Starting the Project

To initiate a project of Django on Your PC, open Terminal and Enter the following command...

Project Structure

A Django Project when initialized contains basic files by default such as manage.py, view.py, etc. A simple project structure is enough to create a single-page application. Here are the major files and their explanations. Inside the geeks_site folder ( project folder ) there will be the following files-...

Creating App

Till now we have created the Django project and now we will create an app. Django is famous for its unique and fully managed app structure. For every functionality, an app can be created like a completely independent module. To create an app we need to go to the directory containing manage.py and run the following command –...

Defining Models

...

Render Model in Django Admin

...

Django Views

...

Django URL Patterns

...

Django Templates

...

Styling Project

After creating and adding data to our models, we need to display the data to our site. Rendering data is a two-step process. First we need to create our view function and then we need to pass the data to our template. Here we will focus on the view function....

Django Forms

...

Django Projects

...