From d372de4d910cc2d9463ba97d76f524c971933c0d Mon Sep 17 00:00:00 2001 From: Lucas Schumacher Date: Sun, 19 May 2024 16:12:02 -0400 Subject: [PATCH] Use html template to generate testapp views --- testapp/templates/testapp/index.html | 15 +++++++++++++++ testapp/views.py | 11 +++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 testapp/templates/testapp/index.html diff --git a/testapp/templates/testapp/index.html b/testapp/templates/testapp/index.html new file mode 100644 index 0000000..557e910 --- /dev/null +++ b/testapp/templates/testapp/index.html @@ -0,0 +1,15 @@ + + + + Testapp + + +

Testapp

+

This is the testapp.

+ {% if click_count %} +

This page was requested {{ click_count }} times.

+ {% else %} +

Error: Could not get request count from database.

+ {% endif %} + + \ No newline at end of file diff --git a/testapp/views.py b/testapp/views.py index 1aa865b..5adeb29 100644 --- a/testapp/views.py +++ b/testapp/views.py @@ -14,7 +14,10 @@ def index_view(request): # Increment the click count click_count.clicks += 1 click_count.save() - # When this view is requested we will respond with this text - text = "Hello World! This is the testapp.
This page was requested " + str(click_count.clicks) + " times." - # Build a Http response with our text and send it to the requester - return HttpResponse(text) + + # This is a dictionary that will be passed to the template + # We will use it to pass the number of clicks to the template + context = {'click_count': click_count.clicks} + + # Render the template and return the response + return render(request, 'testapp/index.html', context) \ No newline at end of file