Add ClickCount model to store how many times testapp has been requested
This commit is contained in:
parent
77df6e2aca
commit
86d2990dfa
@ -31,6 +31,7 @@ ALLOWED_HOSTS = []
|
|||||||
# Application definition
|
# Application definition
|
||||||
|
|
||||||
INSTALLED_APPS = [
|
INSTALLED_APPS = [
|
||||||
|
'testapp.apps.TestappConfig',
|
||||||
'django.contrib.admin',
|
'django.contrib.admin',
|
||||||
'django.contrib.auth',
|
'django.contrib.auth',
|
||||||
'django.contrib.contenttypes',
|
'django.contrib.contenttypes',
|
||||||
|
|||||||
@ -1,3 +1,6 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
|
from .models import ClickCount
|
||||||
|
|
||||||
# Register your models here.
|
# Register your models here.
|
||||||
|
|
||||||
|
admin.site.register(ClickCount)
|
||||||
21
testapp/migrations/0001_initial.py
Normal file
21
testapp/migrations/0001_initial.py
Normal file
@ -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)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
@ -1,3 +1,10 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
# Create your models here.
|
# 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}"
|
||||||
|
|||||||
@ -1,11 +1,21 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
|
|
||||||
|
# Import models that we need to use
|
||||||
|
from .models import ClickCount
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
|
||||||
|
|
||||||
def index_view(request):
|
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
|
# 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.<br>This page was requested " + str(click_count.clicks) + " times."
|
||||||
# Build a Http response with our text and send it to the requester
|
# Build a Http response with our text and send it to the requester
|
||||||
return HttpResponse(text)
|
return HttpResponse(text)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user