Search This Blog

Views in Python Defining a View

 

Defining a View

Views are defined in the file views.py within the app directory.

A sample definition would be

from django.http import HttpResponse

def greeting(request):
    html = "<html><body>Welcome to Django</body></html>"
    return HttpResponse(html)
  • The class HttpResponse is imported from the django.http module.
  • greeting is the view function that takes an HttpRequest object as its first parameter named request.
  • The generated response in an HTML format is returned as an HttpResponse object by the view.

No comments:

Post a Comment

If you have any doubts, Please let us know.