Issue
Good evening, everyone, I'm a beginner at Django I have blog page details that has count views and category for the blog post and blog post tags and other staff
my post model is:
class Post(models.Model):
title = models.CharField(max_length=250)
content = RichTextUploadingField()
post_date = models.DateTimeField(auto_now=True)
slug = models.SlugField(max_length=200, unique=True)
image = models.ImageField(null=True, blank=True, upload_to="images/")
tagss = models.ManyToManyField(Tags, blank=True, related_name='post')
Categorys = models.ManyToManyField(Category, blank=True, related_name='post')
views_count = models.IntegerField(default=0, null=True, blank=True)
def __str__(self):
return self.title
and my view is:
def post_page(request, slug):
post = Post.objects.get(slug = slug)
post.views_count = post.views_count + 1
post.save()
print("views:", post.views_count )
cats = Category.objects.all()
tags = Tags.objects.all()
# change 2 to how many random items you want
random_items = list(Post.objects.all().order_by('?')[:2])
related_posts = list(
Post.objects.filter(
Q(tagss__name__in=[tag.name for tag in post.tagss.all()]) |
Q(Categorys__name__in=[Category.name for Category in post.Categorys.all()])
).distinct()
)
recent_posts = Post.objects.all().order_by('-id')[:4]
context = {'post': post, 'cats': cats, 'tags': tags, 'recent_posts': recent_posts,
'random_blog': random_items, 'related_posts': related_posts}
return render(request, 'blog/Blog_details.html', context)
and that's my url.py
from . import views
from django.urls import path
urlpatterns = [
path('blog', views.blogs, name='blog'),
path('blog/<slug:slug>', views.post_page, name= 'blog_page')
and that's my Template:
{% extends "base.html" %}
{% load static %}
{% block title %}SAS | Cosmetics Blog details {% endblock title %}
{% block content %}
<main id="content" class="wrapper layout-page">
<section class="z-index-2 position-relative pb-2 mb-12">
<div class="bg-body-secondary mb-3">
<div class="container">
<nav class="py-4 lh-30px" aria-label="breadcrumb">
<ol class="breadcrumb justify-content-center py-1 mb-0">
<li class="breadcrumb-item"><a title="Home" href="index.html">Home</a></li>
<li class="breadcrumb-item active" aria-current="page">Blog Update</li>
</ol>
</nav>
</div>
</div>
</section>
<section class="pt-10 pb-16 pb-lg-18">
<div class="container">
<div class="row">
<div class="col-lg-4">
<div class="position-sticky top-0">
<aside class="primary-sidebar mt-12 pt-2 mt-lg-0 pt-lg-0 pe-xl-9 me-xl-2">
<div class="widget widget-search">
<h4 class="widget-title fs-5 mb-6">Search</h4>
<form action="#">
<div class="input-group">
<button type="submit"
class="input-group-text bg-transparent px-4 border-0 position-absolute z-index-4 text-body-emphasis fs-5 start-0 top-0 bottom-0 m-auto"><svg
class="icon icon-magnifying-glass-light">
<use xlink:href="#icon-magnifying-glass-light"></use>
</svg></button>
<input type="search" name="search" class="form-control ps-11"
placeholder="Search">
</div>
</form>
</div>
<div class="widget widget-category">
<h4 class="widget-title fs-5 mb-6">Category</h4>
<ul class="navbar-nav navbar-nav-cate" id="widget_category">
{% for category in cats %}
<li class="nav-item">
<a href="#" title="{{category.name}}"
class="text-reset position-relative d-block text-decoration-none text-body-emphasis-hover d-flex align-items-center"><span
class="text-hover-underline">{{category.name}}</span></a>
</li>
{% endfor %}
</ul>
</div>
<div class="widget widget-post">
<h4 class="widget-title fs-5 mb-6">Recent posts</h4>
<ul class="list-unstyled mb-0 row gy-7 gx-0">
{% for recent in recent_posts %}
<li class="col-12">
<div class="card border-0 flex-row">
<figure class="flex-shrink-0 mb-0 me-7">
<a href="{% url "blog_page" recent.slug %}" class="d-block"
title="{{recent.title}}">
<img data-src="{{recent.image.url}}"
class="lazy-image"
alt="{{recent.title}}" width="60"
height="80" src="#">
</a>
</figure>
<div class="card-body p-0">
<h5
class="card-text fw-semibold ls-1 text-uppercase fs-13px mb-3 text-body text-primary-hover">
<a class="text-decoration-none text-reset" href="{{recent.Categorys.first.slug}}"
title="Skin care">{{recent.Categorys.first.name}}</a></h5>
<h4
class="card-title mb-0 text-body-emphasis fs-15px lh-base text-primary-hover">
<a class="text-decoration-none text-reset"
href="{% url "blog_page" recent.slug %}"
title="{{recent.title}}">{{recent.title}}</a></h4>
</div>
</div>
</li>
{% endfor %}
</ul>
</div>
<div class="widget widget-banner">
<div class="card border-0">
<img class="card-img img-fluid lazy-image"
data-src="{% static "blog/assets/images/blog/widget-banner-img.jpg" %}"
alt="Be your kind of beauty" width="340" height="343" src="#">
<div
class="card-img-overlay pt-8 pb-7 px-xl-14 d-flex flex-column align-items-center justify-content-between">
<h2 class="fw-semibold text-uppercase fs-30px lh-12 text-white text-center">
Be your kind of beauty</h2>
<a href="" target="_blank" class="btn btn-white shadow-none">
Explore More
</a>
</div>
</div>
</div>
<div class="widget widget-tags">
<h4 class="widget-title fs-5 mb-6">Tags</h4>
<ul class="w-100 mt-n4 list-unstyled d-flex flex-wrap mb-0">
{% for tag in tags %}
<li class="me-6 mt-4">
<a href="#" title="{{tag.name}}"
class="text-reset d-block text-decoration-none text-body-emphasis-hover text-hover-underline">{{tag.name}}</a>
</li>
{% endfor %}
</ul>
</div>
<div class="widget widget-instagram">
<div class="d-flex align-items-baseline justify-content-between">
<h4 class="widget-title fs-5 mb-6">Follow us</h4><a href="#"
class="text-primary text-hover-underline">@GRACE</a>
</div>
<div class="row g-10px">
<div class="col-4">
<a class="card-img-overlay-hover hover-zoom-in d-block"
href="{% static "blog/assets/images/instagram/instagram-01.jpg" %} " title="instagram-01"
data-gallery="widget_instagram">
<img class="lazy-image img-fluid w-100" width="106" height="106"
data-src="{% static "blog/assets/images/instagram/instagram-01-106x106.jpg" %} "
alt="instagram-01" src="#">
<span class="card-img-overlay bg-dark bg-opacity-30"></span>
</a>
</div>
<div class="col-4">
<a class="card-img-overlay-hover hover-zoom-in d-block"
href="{% static "blog/assets/images/instagram/instagram-02.jpg" %}" title="instagram-02"
data-gallery="widget_instagram">
<img class="lazy-image img-fluid w-100" width="106" height="106"
data-src="{% static "blog/assets/images/instagram/instagram-02-106x106.jpg" %}"
alt="instagram-02" src="#">
<span class="card-img-overlay bg-dark bg-opacity-30"></span>
</a>
</div>
<div class="col-4">
<a class="card-img-overlay-hover hover-zoom-in d-block"
href="{% static "blog/assets/images/instagram/instagram-03.jpg" %}" title="instagram-03"
data-gallery="widget_instagram">
<img class="lazy-image img-fluid w-100" width="106" height="106"
data-src="{% static "blog/assets/images/instagram/instagram-03-106x106.jpg" %}"
alt="instagram-03" src="#">
<span class="card-img-overlay bg-dark bg-opacity-30"></span>
</a>
</div>
<div class="col-4">
<a class="card-img-overlay-hover hover-zoom-in d-block"
href="{% static "blog/assets/images/instagram/instagram-04.jpg" %}" title="instagram-04"
data-gallery="widget_instagram">
<img class="lazy-image img-fluid w-100" width="106" height="106"
data-src="{% static "blog/assets/images/instagram/instagram-04-106x106.jpg" %}"
alt="instagram-04" src="#">
<span class="card-img-overlay bg-dark bg-opacity-30"></span>
</a>
</div>
<div class="col-4">
<a class="card-img-overlay-hover hover-zoom-in d-block"
href="{% static "blog/assets/images/instagram/instagram-05.jpg" %}" title="instagram-05"
data-gallery="widget_instagram">
<img class="lazy-image img-fluid w-100" width="106" height="106"
data-src="{% static "blog/assets/images/instagram/instagram-05-106x106.jpg" %}"
alt="instagram-05" src="#">
<span class="card-img-overlay bg-dark bg-opacity-30"></span>
</a>
</div>
<div class="col-4">
<a class="card-img-overlay-hover hover-zoom-in d-block"
href="{% static "blog/assets/images/instagram/instagram-06.jpg" %}" title="instagram-06"
data-gallery="widget_instagram">
<img class="lazy-image img-fluid w-100" width="106" height="106"
data-src="{% static "blog/assets/images/instagram/instagram-06-106x106.jpg" %}"
alt="instagram-06" src="#">
<span class="card-img-overlay bg-dark bg-opacity-30"></span>
</a>
</div>
</div>
</div>
</aside>
</div>
</div>
<div class="col-lg-8">
<div class=" text-center mb-13">
{% if post.Categorys.all %}
<a href="#"
class="btn btn-light btn-hover-bg-dark btn-hover-border-dark btn-hover-text-light shadow-none py-0 px-6 mb-6">
{{ post.Categorys.first.name }}
</a>
{% endif %}
<h2 class=" px-6 text-body-emphasis border-0 fw-500 mb-4 fs-3 ">
{{post.title}}
</h2>
<ul
class="list-inline fs-15px fw-semibold letter-spacing-01 d-flex justify-content-center align-items-center">
<li><img data-src="{% static "blog/assets/images/blog/post-detail-header-img.png" %}" alt="Grace"
class="lazy-image img-fluid" src="#"></li>
<li class="border-end px-6 text-body-emphasis border-0 text-body">
By
<a href="#">Admin</a>
</li>
<li class="list-inline-item px-6">{{post.post_date|date:"M d, Y"}}</li>
<li class="ms-5 list-style-disc">{{post.views_count}} view{{post.views_count|pluralize}}</li>
</ul>
</div>
<div class="post-content">
<img data-src="{{post.image.url}}" width="770" height="470" alt=""
class="lazy-image mb-12 img-fluid" src="#">
{{post.content|safe}}
</div>
<div class="row no-gutters pt-11 justify-content-sm-between">
<div class="col-sm-6 mb-4 mb-sm-0">
<ul class="list-inline fw-semibold">
{% for tag in post.tagss.all %}
<li class="list-inline-item me-3">
<a href=""
class="text-body text-body-emphasis-hover text-decoration-none">#{{tag.name}}</a>
</li>
{% endfor %}
</ul>
</div>
<div class="col-sm-6 d-flex justify-content-sm-end">
<label class="text-secondary fw-semibold me-7 mb-0">Share:</label>
<ul class="list-inline mb-0 lh-1">
<li class="list-inline-item me-7">
<a href="" class="fs-18px lh-14 fw-normal">
<i class="fa-brands fa-twitter"></i>
</a>
</li>
<li class="list-inline-item me-7">
<a href="" class="fs-18px lh-14 fw-normal">
<i class="fa-brands fa-facebook-f"></i>
</a>
</li>
<li class="list-inline-item me-7">
<a href="" class="fs-18px lh-14 fw-normal">
<i class="fa-brands fa-instagram"></i>
</a>
</li>
<li class="list-inline-item me-7">
<a href="" class="fs-18px lh-14 fw-normal">
<i class="fa-brands fa-youtube"></i>
</a>
</li>
</ul>
</div>
<div class="col-12 mt-5 mb-7">
<div class="border-bottom"></div>
</div>
{% if random_blog %}
<div class="col-sm-6">
<div class="ml-8">
<p class="fs-13px ls-1 text-uppercase mb-5 fw-semibold px-8">
{{ random_blog.0.Categorys.first.name }}
</p>
<a href="{{ random_blog.0.slug }}" class="fs-15px fw-semibold position-relative px-8">
<i class="far fa-chevron-left mt-2 position-absolute start-0 top-0"></i>
{{ random_blog.0.title }}
</a>
</div>
</div>
<div class="col-sm-6 d-flex justify-content-sm-end">
<div class="mr-8 text-right">
<p class="fs-13px text-start text-sm-end ls-1 text-uppercase mb-5 fw-semibold px-8">
{{random_blog.1.Categorys.first.name}}
</p>
<a href="{{ random_blog.1.slug }}"
class="fs-15px text-start text-sm-end fw-semibold position-relative px-8">
<i class="far fa-chevron-right mt-2 position-absolute end-0 top-0"></i>
{{ random_blog.1.title }}
</a>
</div>
</div>
{% endif %}
</div>
{% if related_posts %}
<div class="pt-14 pb-13 pb-lg-15 pt-lg-18 mx-n5" id="post_related">
<div class="container">
<div class="text-center">
<h2 class="mb-6 fs-3">Related Posts</h2>
</div>
</div>
<div class="container container-xxl mt-10 pt-3">
<div class="slick-slider"
data-slick-options="{"arrows":false,"dots":true,"responsive":[{"breakpoint":1200,"settings":{"slidesToShow":3}},{"breakpoint":992,"settings":{"dots":true,"slidesToShow":2}},{"breakpoint":768,"settings":{"dots":true,"slidesToShow":1}}],"slidesToShow":3}">
{% for post in related_posts %}
<div>
<article class="card card-post-grid-3 bg-transparent border-0"
data-animate="fadeInUp">
<figure class="card-img-top mb-8 position-relative"><a href="{{post.slug}}"
class="hover-shine hover-zoom-in d-block"
title="{{post.title}}">
<img data-src="{{post.image.url}}"
class="img-fluid lazy-image w-100"
alt="Our Favorite Multitasking Products" width="237"
height="288" src="#">
</a></figure>
<div class="card-body p-0">
<ul
class="post-meta list-inline lh-1 d-flex flex-wrap fs-13px text-uppercase ls-1 fw-semibold m-0">
<li class="list-inline-item"><a
class="text-reset text-decoration-none text-primary-hover"
href="#" title="{{post.Categorys.first.name}}">{{post.Categorys.first.name}}</a></li>
</ul>
<h4 class="card-title fs-6 lh-base mt-5 pt-2 mb-0">
<a class="text-decoration-none" href="{{post.slug}}"
title="{{post.title}}">{{post.title}}</a>
</h4>
</div>
</article>
</div>
{% endfor %}
</div>
</div>
</div>
{% endif %}
</div>
</div>
</div>
</section>
</main>
{% endblock content %}
my problem is when I refresh the post page the views increase by 2 I need when the user refresh the page increase by 1 Thanks everyone
Solution
The issue is about calling the post_view() view function twice. There are some common scenarios that causes that problem:
- Your post_view() function might be triggered when modern browsers sends ´OPTIONS´ and ´GET´ requests together. You can change your code as this for only handling ´GET´ requests:
def post_page(request, slug):
post = get_object_or_404(Post, slug=slug) # This is a cleaner way to get the object or 404
if request.method == 'GET':
# Increment view count only if the request is a GET request
Post.objects.filter(slug=slug).update(views_count=F('views_count') + 1)
post.refresh_from_db() # Refresh the instance after the update
... rest of your code ...
return render(request, 'blog/Blog_details.html', context)
- Please inspect this, it might be about your template files: Misusing img tags causes double calling It might be about favicon, also: Misusing favicon link tag
Answered By - M Burak
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.