Answer the question
In order to leave comments, you need to log in
How to use django-hitcount with ListView class?
I need to create a hit counter on the main page of a website. I learned about a good solution in the form of django-hitcount , the problem is that in the documentation it is used everywhere to add a counter to a specific model, and I can’t figure out how to use it in my situation.
Here is my view (It uses a lot of things: django-filter, form, pagination for filtered querisets, that's why the class is so big):
class MainPage(ListView):
model = DailyOrders
template_name = 'Catalog/main_page.html'
context_object_name = 'orders'
paginate_by = 10
allow_empty = True
def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = ClientsForm()
context['filter'] = DailyOrdersFilter(self.request.GET, queryset = DailyOrders.objects.all())
return context
def post(self, request):
if request.method == 'POST':
form = ClientsForm(request.POST)
if form.is_valid():
form.save()
send_mail(subject=form.cleaned_data['FIO'],
message=form.cleaned_data['phone'],
from_email='www.mail.ru',
recipient_list=['[email protected]']
)
messages.success(request, 'Заявка отправлена')
return redirect('main')
else:
messages.error(request, 'Форма заполнена неверно')
form = ClientsForm()
return redirect('main')
def get_queryset(self):
queryset = super().get_queryset()
return DailyOrdersFilter(self.request.GET, queryset=queryset).qs
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question