django-hitcount icon indicating copy to clipboard operation
django-hitcount copied to clipboard

Making request using JavaScript, hits is incrementing every request + 1

Open cakctus opened this issue 1 year ago • 0 comments

Hello,

here is my django model

class HitCount(models.Model, HitCountMixin):

endpoint = models.CharField(max_length=255000000000000000)
hits = models.PositiveIntegerField(default=0)

def __str__(self):
    return self.endpoint

here is my views

class CustomHitCountDetailView(HitCountDetailView): model = HitModel count_hit = True

@method_decorator(cache_control(no_cache=True, must_revalidate=True, no_store=True))
def dispatch(self, *args, **kwargs):
    return super().dispatch(*args, **kwargs)

def get_object(self):
    endpoint_path = self.kwargs.get('endpoint_path')
    hit_count, created = HitModel.objects.get_or_create(
        endpoint=endpoint_path)
    return hit_count

def render_to_response(self, context, **response_kwargs):

    print("Hit Count Object:", self.object.endpoint)

    hit_count = self.object.hits if hasattr(self, 'object') else 0
    hits = HitCount.objects.get(id=self.object.id)
    print(hits.hits)
    response_data = {
        'endpoint': self.object.endpoint if hasattr(self, 'object') else None,
        'hit_count': hit_count,
        'id': self.object.id,
        # 'hits': get_object_or_404(HitCount, id=self.object.id).hits
        'hits_id': hits.id,
        'hits': self.object.hit_count.hits
    }

    return JsonResponse(response_data)

if i make reqeust from browser -> http://127.0.0.1:8000/api/hitcount/66/ hits is not incrementing but if i make request from axios (javascript) every time when i made reqeust, hits is incrementing + 1

cakctus avatar Aug 29 '23 15:08 cakctus