Add page view counter

To showcase database connectivity.
This commit is contained in:
Rodolfo Carvalho 2015-05-25 16:12:22 +02:00
parent d398a46623
commit 86f8f20776
4 changed files with 35 additions and 2 deletions

View file

@ -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)),
],
),
]

View file

@ -1,3 +1,7 @@
from django.db import models from django.db import models
# Create your models here. # Create your models here.
class PageView(models.Model):
hostname = models.CharField(max_length=32)
timestamp = models.DateTimeField(auto_now_add=True)

View file

@ -50,7 +50,8 @@
<div id="hostname"> <div id="hostname">
<p> <p>
Server hostname: {{ HOSTNAME }} Server hostname: {{ HOSTNAME }}<br>
Page views: {{ count }}
</p> </p>
</div> </div>
</body></html> </body></html>

View file

@ -1,7 +1,14 @@
import os import os
from django.shortcuts import render from django.shortcuts import render
from .models import PageView
# Create your views here. # Create your views here.
def index(request): 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()
})