samedi 25 juin 2016

Django - How do i include several elements in pagination


This is my first project in django and I've been following several different tutorial, most of which are very useful. I've made a very simple blog as my first app and now I'm expanding it a bit beyond what the tutorials have shown me. Namely, I added new model, Category, which every post can belong to. The model itself and all related stuff works. The only thing that I'm now having problem with is resolving URLs. I'm using pagination to normally list posts, but I would like to add additional and optional element to it, namely category. So when browsing through the list I not only have the options of site.com/posts/?page=2 but can also use something like site.com/posts/?cat=news&page=2

models.py

# Model for blog post category
class Category(models.Model):
    title = models.CharField(max_length=120)
    slug = models.SlugField(unique=True)
    description = models.TextField()

    class Meta:
        verbose_name_plural = "Categories"

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"slug": self.slug})
# Model for blog post
class Post(models.Model):
    title = models.CharField(max_length=120)
    slug = models.SlugField(unique=True)
    category = models.ManyToManyField(Category, blank=True, through='CategoryToPost')
    content = models.TextField()
    image = FilerImageField(null=True, blank=True, related_name="post_image")
    created = models.DateTimeField(auto_now=False, auto_now_add=True)
    published = models.DateTimeField(auto_now=False, auto_now_add=True)
    updated = models.DateTimeField(auto_now=True, auto_now_add=False)

    def __str__(self):
        return self.title

    def Category(self):
        return self.category.all()

    def get_absolute_url(self):
        return reverse("posts:detail", kwargs={"slug": self.slug})
# Model for assigning post to a category
class CategoryToPost(models.Model):
    post = models.ForeignKey(Post)
    category = models.ForeignKey(Category)

urls.py

from .views import (
    post_list,
    post_detail
    )

urlpatterns = [
    url(r'^$', post_list),
    url(r'^(?P<slug>[w-]+)/$', post_detail, name='detail'),
]

Note on urls, the line from project urls.py is url(r'^posts/', include("posts.urls", namespace='posts')),

views.py

def post_detail(request, slug):

    instance = get_object_or_404(Post, slug=slug)

    context = {
        "title": instance.title,
        "instance": instance,
    }

    return render(request, "post_single.html", context)

def post_list(request):

    queryset_list = Post.objects.all().order_by("-published")
    paginator = Paginator(queryset_list, 5)

    page = request.GET.get('page')
    try:
        queryset = paginator.page(page)
    except PageNotAnInteger:
        queryset = paginator.page(1)
    except EmptyPage:
        queryset = paginator.page(paginator.num_pages)

    context = {
        "title": "Post List",
        "object_list": queryset,
    }

    return render(request, "post_list.html", context)

I know that I need to add this somewhere inside post_list, but to be honest I'm not even sure where to start and django pagination documentation isn't very clear on this.


Aucun commentaire:

Enregistrer un commentaire