Every Course in $5, Hurry Up!

What are views in Django | Django Tutorials

What are views in Django copy
Django Django Tutorials

What are views in Django | Django Tutorials

What are views in Django | Django Tutorials

Django is followed MVT architectural design pattern which stands for model view and template. In MVT, V stands for view, view is the section or part in Django in which we write logic, render a template, go request, receive a response.  You should know, when the user hits the URL it creates a request and submit it to the webserver through the view section and receive a response in the form of displaying the template.

Actually, the view is the Python function in which the server gets a request and gives a response. Basically, the request is created when the user hits the URL which is defined in the urls.py file. The user receives a response in HTML content in the views function. It may a  web page, 404 not found page, XML document like sitemap, etc.

. If you want to learn more about URL patterns in Django, we published an explained tutorial on it.

What is Django URL patterns | Django Tutorials

How to Make Django Dynamic URLs Pattern | Django Tutorials

A simple example of views in the Django project

from django.http import HttpResponse
def jafricode(request):
         msg= "Hello world!"
         return HttpResponse(msg)

Code Explanation:

First of all, you have to import HttpResponse from django.http file. Without importing, your function will not work and will show an error of an undefined function.

So here is the function base view name is “jafricode”, in the parenthesis, there is one argument “request” which is taking requests from user to webserver. Then we set a variable msg giving some text base value. When the webserver gives a response, we say it, HttpResponce display value of msg variable.

There are different ways to write your Django project logic in views. There are two types of views, class-based and function bases. Both have their own importance, you can take any one of them. It depends upon your requirement. Because class-based views are easy to use and save development time than function base. But sometimes you also need to use function base view.

The example we share below these paragraphs is the function base. We will write a tutorial that explains its differentiation between class base views and function base with examples.

Another example, you will learn how to render a Django template in the function-based view.

def detail(request):
    return render(request, 'polls/detail.html')

Code Explanation:

You can analyze that, there is a return keyword we used before the detail function views. Then we use the render function which is taking two arguments, one is requested which requests the webserver, and the other is the template address that is to be rendered in the browser on receiving a response from the webserver. When this view function takes a response, it may be in a different form as HTML document as the web page, not found web page or XML page. So users receive responses in different ways from the server.

Conclusion

I hope you have understood, What are views in Django, and you have understood what’s important to learn. Because it is the major part or main topic you need to learn its properly. If you don’t view the section while learning Django then you have missed more topics.

After reading this Django tutorial, if you have any problem or confusion then please ask us, we will find the best for you.

If you use Facebook, Twitter, Pinterest, LinkedIn, or other social media platforms, then share this Django tutorial, if you share, more students will take the chance to learn.

Thank you!

Recommended Django tutorials for beginners

What is Django template | Django Tutorials

What are Static files in Django Project | Django Tutorials

Complete Django Template Language Tutorial (DTL)

Leave your thought here

Your email address will not be published. Required fields are marked *

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the comparison bar
Compare