from django.shortcuts import render from django.http import HttpResponse # Import models that we need to use from .models import ClickCount # Create your views here. def index_view(request): # Get the number of clicks from the database click_count = ClickCount.objects.first() # If the click_count is None, then we have not yet clicked if click_count is None: click_count = ClickCount(0) # Increment the click count click_count.clicks += 1 click_count.save() # 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)