brand/views.py and add following code to
HttpResponse is buitlin utility function provided by django to return HttpResponse to incoming request. We can write a complete HTML code in HttpResponse function but the readability of code will be minimal and it will be more difficult to debug. It will be better to use HTML pages instead of writing complete HTML codes into HttpResponse function. This is where render function comes into play.
render is builtin utility provided by Django to render HTML pages and to feed dynamic content into them. render function takes three input parameters normally.
- First parameter is request parameter which was received by our function.
- Second parameter is url of an HTML page which will be displayed on screen on invoking the current function.
- Third parameter which is optional but very important and which makes our HTML page dynamic is a dictionary which is sent to HTML page as key-value pair.
In our homepage function, we have used render function without third parameter. Before running the server, make sure that you have configured url in your
geeks_site/settings.py by adding following lines of code.
from brand.views import ViewDemo, homepage urlpatterns = [ path('', homepage), path('admin/', admin.site.urls), path('hello-world/', ViewDemo), ]
If you run command
python manage.py runserver now, you will get following error page:
This error clearly states that the HTML page we are trying to view doesn’t exist. So our next step is to add a HTML page named homepage.html in our
Refresh the page and output is displayed on your screen.
- Django Templates | Set - 1
- Django Templates
- variables - Django Templates
- Django Tutorial
- Django Models | Set - 1
- Django Models | Set - 2
- Django Models
- Django Forms
- Django Basics
- url - Django Template Tag
- Django Formsets
- Django ModelFormSets
- How to Create an App in Django ?
- CharField - Django Forms
- EmailField - Django Forms
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.