Loading...

Programación Django

Programación Django

from django.views.generic import ListView, DetailView
from django.shortcuts import get_object_or_404
from .models import Post, Categoria, Etiqueta

class ListaNoticias(ListView):
    model = Post
    template_name = 'advanced_blog/post_list.html'
    context_object_name = 'noticias'
    paginate_by = 10

    def get_queryset(self):
        queryset = Post.objects.filter(estado='publicado').select_related('autor')
        query = self.request.GET.get('q')
        if query:
            queryset = queryset.filter(
                Q(titulo__icontains=query) |
                Q(contenido__icontains=query)
            )
        return queryset

class DetalleNoticia(DetailView):
    model = Post
    template_name = 'advanced_blog/post_detail.html'
    context_object_name = 'noticia'

    def get_object(self, queryset=None):
        noticia = super().get_object(queryset)
        noticia.vistas += 1
        noticia.save()
        return noticia
 

Share:
Profile Pic

GauravNagar

I am a Python enthusiast. I do lot of experiments with available libraries and develop scripts.

0 Comments

Leave Your Comment