Full Guide Function-based views Django | Complete Django Tutorials

Function-based views Django copy
Django Django Tutorials

Full Guide Function-based views Django | Complete Django Tutorials

Function-Based Views Django | Django Tutorials

As in the previous tutorials, we have discussed what is views in Django. Actually, views have two styles in which we write coding to achieve the required functionalities. One of them, there is Function-based views Django, in which we have to create a function to get desired functionality. And you should know Django follows the MVT architecture design pattern, in MVT, V is the views in which we write logic, functionality for a Django project. We also render a template in the views section. All the views coding is written in the views.py file which is generated by default when you build a Django application.

So, in this Django tutorial, we will move around function-based views Django Style. Before describing this, and as I have discussed that Django has two ways to for writing views code that is:

  • Function-Based views 
  • Class-Based views 

If you search on the internet to get information related to Django views, So, you will notice that some content writers use FBVs and CBVs. So don’t worry, these are not new terms but they are the same things that types of views.

Examples Related to Function-Based Views Django

In this Django tutorial, we have to discuss function-based views. In FBVs, you have to make a function as we define in Core python. We start with keyword ‘def’ with function name passing one argument ‘request’.

Another important line in the FBVs is the rendering of a template.

def detail_view(request):
          context ={‘name’:’JafriCode’}
          return render(request, “detail_view.html”, context)

The above example shows that there is one function ‘detail_view’ which taking one argument request. It means when the user hits a URL as you have defined in the urls.py file and views reference linked with that URL In the URL pattern, so that, view reference goes to the views.py file to execute that function. So, on hitting URL such a request go to the webserver, waiting for a response from the server. After a response template is rendered that shows on the user browser, this is the response of the server that is given to the user.

In short, it is the game of request and response which is working in the views function. The request is submitted by a user to server and response is given by the server to the user.

We share another example, in which another argument will be pass to show that a value is coming from a URL. That argument will be taken in the view logic to use according to situations.

def std_view(request, id):

           student = Students.objects.get(id = id)

           context = {‘std’:student }

           return render(request, “std_view.html”, context)

In this case, you are seeing, there is another argument id is passing in the function. This id is coming from the URL, this id will match to getting a specific student. For example in the URL www.domain.com/student/id=2. So in this case id is equal to 2, so this 2 will match with student id and that specific student record will come to the user screen.

Conclusion

I hope you have understood about function-based views Django topic. It will be cleared to you because we want to explain it with examples as possible. Actually, keep in mind, function-based views are a very simple and easy way to work with Django. Because it has no complex type of structure but just need to learn how to use built-in functions etc in it and other desired logic.

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.

After reading today’s topic, if you face any problem while reading, understanding please discuss it with us. If you have understood and have any relevant questions then please discuss with us in the following comment section

Recommended Django Tutorial for Beginners

Complete Guide on Django ORM | Django Tutorials

Class-based views Django | Django Tutorials

Django Tutorial for Beginners | Django Tutorials series 

Create Django Form with CSRF Token | Django Tutorials

Top 20 Python Real World Applications | Python tutorials

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

Get your Enrollment

50%OFF

Leave your details below and receive a discount coupon in your inbox