About views file in django with example


                 A views is a python function which takes HTTPRequest and gives HTTPResponse

  • A view is a responsible to perform some arbitrary logic 

  • A view will interact with model.py file and with template file.

  • A view interacts with models.py to manage the data in the database.

  • A view interacts with template file to manage the html code(presentation code)

  • A view will also interact with urls.py file to map the given urls by the users.
  • we define a view by using ‘def’ Keyword.

Steps to create a file (using views file)

Step 1: Create a folder with name ‘Tech’

Step 2: Open folder and type CMD in the path to open command prompt

Step 3: Create a project

   E:Tech>django-admin startproject website

Step 4: Change the path and create application with name technical_teacher

  E:Tech>cd website

  E:Techwebsite> python manage.py startapp technical_teacher

Step 5: Open PYcharm

     File–>open–>select your project–>select ok

Step 6:Go to technical_teacher/views.py file and write the below code.

from django.shortcuts import render

from django.http import HttpResponse

def home(request):
      X=’hello friends welcome to muosigma’
      return HttpResponse(x)

goto website/urls.py file and write the below code

from django.contrib import admin

from django.conf.urls import url

from technical_teacher import views

urlpatterns = [
    url(r’^admin/’, admin.site.urls),
next runserver by using command
E:Techwebsite>python manage.py runserver

copy address and open any browser and paste.
now you should enter
it will show
hello friends welcome to Technical_Teacher

Leave a Reply

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