Browse Source

added visitcounter

master
Domagoj Zecevic 2 years ago
parent
commit
f205ccce05
  1. 8
      blog/admin.py
  2. 20
      blog/migrations/0009_visit.py
  3. 3
      blog/models.py
  4. 6
      blog/views.py
  5. BIN
      db.sqlite3

8
blog/admin.py

@ -1,5 +1,5 @@
from django.contrib import admin from django.contrib import admin
from .models import Post, Index_Post, Sexy_Posts from .models import Post, Index_Post, Sexy_Posts, Visit
class PostAdmin(admin.ModelAdmin): class PostAdmin(admin.ModelAdmin):
list_display = ('title', 'short_description', 'pub_date', 'show_on_page') list_display = ('title', 'short_description', 'pub_date', 'show_on_page')
@ -10,6 +10,10 @@ class IndexPostAdmin(admin.ModelAdmin):
class SexyPost(admin.ModelAdmin): class SexyPost(admin.ModelAdmin):
list_display = ('title', 'short_description', 'pub_date', 'show_on_page') list_display = ('title', 'short_description', 'pub_date', 'show_on_page')
class VisitAdmin(admin.ModelAdmin):
list_display = ('id', 'count',)
admin.site.register(Post, PostAdmin) admin.site.register(Post, PostAdmin)
admin.site.register(Index_Post, IndexPostAdmin) admin.site.register(Index_Post, IndexPostAdmin)
admin.site.register(Sexy_Posts, SexyPost) admin.site.register(Sexy_Posts, SexyPost)
admin.site.register(Visit,VisitAdmin)

20
blog/migrations/0009_visit.py

@ -0,0 +1,20 @@
# Generated by Django 4.2.7 on 2024-01-28 21:36
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('blog', '0008_index_post_show_on_page_sexy_posts_show_on_page'),
]
operations = [
migrations.CreateModel(
name='Visit',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('count', models.IntegerField(default=0)),
],
),
]

3
blog/models.py

@ -26,5 +26,8 @@ class Sexy_Posts(models.Model):
pub_date = models.DateTimeField('date published') pub_date = models.DateTimeField('date published')
show_on_page = models.BooleanField(default=False) show_on_page = models.BooleanField(default=False)
class Visit(models.Model):
count = models.IntegerField(default=0)
def __str__(self): def __str__(self):
return self.title return self.title

6
blog/views.py

@ -1,10 +1,14 @@
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404
from .models import Post, Index_Post, Sexy_Posts from .models import Post, Index_Post, Sexy_Posts, Visit
def index(request): def index(request):
index_posts = Index_Post.objects.all() index_posts = Index_Post.objects.all()
visit, created = Visit.objects.get_or_create(pk=1)
visit.count += 1
visit.save()
return render(request, 'blog/index.html', {'index_posts': index_posts}) return render(request, 'blog/index.html', {'index_posts': index_posts})
def post_list(request): def post_list(request):
posts = Post.objects.all() posts = Post.objects.all()
return render(request, 'blog/post_list.html', {'posts': posts}) return render(request, 'blog/post_list.html', {'posts': posts})

BIN
db.sqlite3

Binary file not shown.
Loading…
Cancel
Save