Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cakctus opened this issue Aug 29, 2023 · 0 comments
Open

Comments

@cakctus
Copy link

cakctus commented Aug 29, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant