Views
Introduction#
A view function, or view for short, is simply a Python function that takes a Web request and returns a Web response. -Django Documentation-
[Introductory] Simple View (Hello World Equivalent)
Let’s create a very simple view to respond a “Hello World” template in html format.
-
To do that go to
my_project/my_app/views.py
(Here we are housing our view functions) and add the following view:from django.http import HttpResponse def hello_world(request): html = "<html><title>Hello World!</title><body>Hello World!</body></html>" return HttpResponse(html)
-
To call this view, we need to configure a url pattern in
my_project/my_app/urls.py
:from django.conf.urls import url from . import views urlpatterns = [ url(r'^hello_world/$', views.hello_world, name='hello_world'), ]
-
Start the server:
python manage.py runserver
Now if we hit
https://localhost:8000/hello_world/
, our template (the html string) will be rendered in our browser.