diff --git a/djangoTest/settings.py b/djangoTest/settings.py index 47a94cb..f8e8436 100644 --- a/djangoTest/settings.py +++ b/djangoTest/settings.py @@ -31,6 +31,7 @@ ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ + 'testapp.apps.TestappConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', diff --git a/testapp/admin.py b/testapp/admin.py index 8c38f3f..91666ea 100644 --- a/testapp/admin.py +++ b/testapp/admin.py @@ -1,3 +1,6 @@ from django.contrib import admin +from .models import ClickCount # Register your models here. + +admin.site.register(ClickCount) \ No newline at end of file diff --git a/testapp/migrations/0001_initial.py b/testapp/migrations/0001_initial.py new file mode 100644 index 0000000..59dd3b0 --- /dev/null +++ b/testapp/migrations/0001_initial.py @@ -0,0 +1,21 @@ +# Generated by Django 4.2.11 on 2024-05-19 18:39 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='ClickCount', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('clicks', models.IntegerField(default=0)), + ], + ), + ] diff --git a/testapp/models.py b/testapp/models.py index 71a8362..e259297 100644 --- a/testapp/models.py +++ b/testapp/models.py @@ -1,3 +1,10 @@ from django.db import models # Create your models here. + +# This is a simple model that stores a click count +class ClickCount(models.Model): + clicks = models.IntegerField(default=0) + # This method provices a string representation of the model + def __str__(self): + return f"ClickCount: {self.clicks}" diff --git a/testapp/views.py b/testapp/views.py index 05ad80e..15ca5a6 100644 --- a/testapp/views.py +++ b/testapp/views.py @@ -1,11 +1,21 @@ 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(1) + click_count.save() + # 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." + 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)