Function Based View in Django

If you want to display HTML, Text, files & more on front-end of your django project, then you will certainly have to learn Function based view in Django.

In this tutorial, you will know some easy steps to create single & multiple view function for you web application. So that you can easily show static or dynamic data on your application web page based on URL requests.

Suppose that you have to show “Welcome to CodingStatus” on the URL “/codingstatus” and “Know Everything About CodingStatus” on URL request “about-us”. Then you can do it by creating view of your applications.

Don’t worry, this example will be executed in the upcoming steps with complete explanation. So, Keep continue reading without skipping any points

Before getting started this topic, you must have well understanding of these topics –

How to Create Django Project

How to Create Django

Django Function-Based View

A Function-Based View is created with the help of a python function that is called a view function and It returns an HTTP response on an HTTP request.

An Http Response is an object that is represented as httpRequest and passed as first arguments of the view function.

An HTTP response is also an object that is represented as HttpResponse and It can accept HTML, XML documents, text, images, video, files, & more

A Function based is also known with other alternative names like view or view function of an app.

Syntax –

A view function is created in views.py file of a Django app with the help of this syntax –

def functionName(request):
    return HttpResponse("Write your Contenr")

Example –

def welcome(request):
   return HttpResponse("<h1>Welcome to Django Tutorials</h1>")

Single Function Based View

If you need a single view of your app then you can create a single function based view

def welcome(request):
    return HttpResponse("<h1>Welcome to CodingStatus</h1>"

Multiple Function Based View

If you need multiple views of your app then you can create multiple function based views

def welcome(request):
   return HttpResponse("<h1>Welcome to CodingStatus</h1>")

def about(request):
  return HttpResponse("<p>Codingstatus is the best website for learning to develop the best web applications</p>")

Steps to Create Django Function-based view

Now, You will learn to create some function based views steb by step with an example. So, let’s start –

1. Create Django Project & App

First of all, you have to create an django project with the name of “myproject” using this command –

django-admin startproject myproject

Now, Go to root diectory of myproject and run this command-

myproject>python manage.py startapp tutorials

After that, you will get the following folder structure –

myproject
  |__myproject
  |__ tutorials
  |     |__ migrations/
  |     |      |__ __init__.py
  |     |__ __init__.py
  |     |__ admin.py
  |     |__ apps.py
  |     |__ models.py
  |     |__ tests.py
  |     |__ views.py
  |__ manage.py

2. Register Django App in settings.py

Open settings.py of inner project folder and register tutorials app within INSTALLED_APPS array –

INSTALLED_APPS = [
    'tutorials',
]

3. Create View functions in views.py

Now, create some view functions in views.py. But you will get a default code in the first line. So, you need not to remove it. just define your custom function below it.

File Name – views.py

from django.shortcuts import render

# Create your views here.
def python_tutorial(request):
  return Httpresponse("<p>Get Python Tutorial</p>")

def django_tutorial(request):
  return HttpResponse("<p>Get Djnago Tutorial</p>")

4. Create Views URL in urls.py

Now, you have to create urls for the created views in urls.py of the inner project folder

File Name – urls.py

from django.contrib import admin
from django.urls import path
from tutorials import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('python-tutorial/', views.python_tutorial),
    path('django-tutorial/', views.django_tutorial),
]

5. Run Django Project to display view page

You have to run django project using this command if you have to display information on the view page

python manage.py runserver

After that ener these urls in your browser

http://localhost:8000/python-tutorial

http://localhost:8000/django-tutorial