From 86f8f207764f35a4a18c6e6d3ac71b49e182bcd6 Mon Sep 17 00:00:00 2001 From: Rodolfo Carvalho Date: Mon, 25 May 2015 16:12:22 +0200 Subject: [PATCH] Add page view counter To showcase database connectivity. --- openshift/migrations/0001_initial.py | 21 +++++++++++++++++++++ openshift/models.py | 4 ++++ openshift/templates/openshift/index.html | 3 ++- openshift/views.py | 9 ++++++++- 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 openshift/migrations/0001_initial.py diff --git a/openshift/migrations/0001_initial.py b/openshift/migrations/0001_initial.py new file mode 100644 index 0000000..d607430 --- /dev/null +++ b/openshift/migrations/0001_initial.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='PageView', + fields=[ + ('id', models.AutoField(verbose_name='ID', auto_created=True, serialize=False, primary_key=True)), + ('hostname', models.CharField(max_length=32)), + ('timestamp', models.DateTimeField(auto_now_add=True)), + ], + ), + ] diff --git a/openshift/models.py b/openshift/models.py index 71a8362..8567d5d 100644 --- a/openshift/models.py +++ b/openshift/models.py @@ -1,3 +1,7 @@ from django.db import models # Create your models here. + +class PageView(models.Model): + hostname = models.CharField(max_length=32) + timestamp = models.DateTimeField(auto_now_add=True) diff --git a/openshift/templates/openshift/index.html b/openshift/templates/openshift/index.html index c634822..a2a3a2a 100644 --- a/openshift/templates/openshift/index.html +++ b/openshift/templates/openshift/index.html @@ -50,7 +50,8 @@

- Server hostname: {{ HOSTNAME }} + Server hostname: {{ HOSTNAME }}
+ Page views: {{ count }}

diff --git a/openshift/views.py b/openshift/views.py index 81d899d..5928757 100644 --- a/openshift/views.py +++ b/openshift/views.py @@ -1,7 +1,14 @@ import os from django.shortcuts import render +from .models import PageView + # Create your views here. def index(request): - return render(request, 'openshift/index.html', {'HOSTNAME': os.getenv('HOSTNAME', 'unknown')}) + hostname = os.getenv('HOSTNAME', 'unknown') + PageView.objects.create(hostname=hostname) + return render(request, 'openshift/index.html', { + 'HOSTNAME': hostname, + 'count': PageView.objects.count() + })