I don't have as my parameter in the function in the view (I only have the request), how do I get the specific post


mike bra

I am trying to display the number of comments in the index page just like it is displayed in the posts page. However, my def index has no parameters.

For my post.html I can display the number of comments like this

  #for single-post page
    def post(request, slug):
            post = get_object_or_404(Post, slug=slug)

            comments_count = Comment.objects.filter(post=post).count()

            context_dict = {

                'comments_count':comments_count,


            }
            return render(request, 'main/post.html', context_dict)

in the template

{{comments_count}}

I want to do the same with my index page.

def index(request):
        try:
                sort = request.GET["sort"].strip()
                sort_method = SortMethods[sort]
                page = request.GET["page"].strip()
        except KeyError:
                sort_method = SortMethods.score
                page = 1

        if sort_method == SortMethods.date:
                post_list = Post.objects.order_by("-pub_date")
        else:
                post_list = Post.objects.all()
                post_list = sorted(post_list, key=lambda x: x.get_score(), reverse=True)

        paginator = Paginator(post_list, 30)

        try:
                posts = paginator.page(page)
        except PageNotAnInteger:
                posts = paginator.page(1)
        except EmptyPage:
                posts = paginator.page(paginator.num_pages)


        context = {
                "posts": posts,
                "pages": paginator.page_range,
                "sort": sort_method.name,
        }
        return render(request, "main/index.html", context)
Selcuk

The easiest way is to add a method to count the annotations in the Postmodel :

class Post(models.Model):
    ...
    def comments_count(self):
        return Comment.objects.filter(post=self).count()

Then use it in your template like:

{% for post in posts %}
    {{ post.title }}: {{ post.comments_count }}
{% endfor %}

Related


I don't have a "DATA" attribute in my view.py request

David I am trying to pass data using jsfunction UpdateUserOrder $(document).ready(function() { document.querySelectorAll('.update-cart').forEach(item =>{ item.addEventListener('click', () => { var productId = item.dataset.product

I don't have a "DATA" attribute in my view.py request

David I am trying to pass data using jsfunction UpdateUserOrder $(document).ready(function() { document.querySelectorAll('.update-cart').forEach(item =>{ item.addEventListener('click', () => { var productId = item.dataset.product

I don't have a "DATA" attribute in my view.py request

David I am trying to pass data using jsfunction UpdateUserOrder $(document).ready(function() { document.querySelectorAll('.update-cart').forEach(item =>{ item.addEventListener('click', () => { var productId = item.dataset.product

I don't have a "DATA" attribute in my view.py request

David I am trying to pass data using jsfunction UpdateUserOrder $(document).ready(function() { document.querySelectorAll('.update-cart').forEach(item =>{ item.addEventListener('click', () => { var productId = item.dataset.product

I don't have a "DATA" attribute in my view.py request

David I am trying to pass data using jsfunction UpdateUserOrder $(document).ready(function() { document.querySelectorAll('.update-cart').forEach(item =>{ item.addEventListener('click', () => { var productId = item.dataset.product

How do I backup data that I don't have permission to?

MMCXCVII Fedora doesn't start anymore, so I need to backup my data before running Startup Repair. The problem is: I can't access my fedora personal files from boot-repair due to lack of permissions. So how do I backup it? anonymous From your live CD, try loggi

How do I backup data that I don't have permission to?

MMCXCVII Fedora doesn't start anymore, so I need to backup my data before running Startup Repair. The problem is: I can't access my fedora personal files from boot-repair due to lack of permissions. So how do I backup it? anonymous From your live CD, try loggi

I don't have it now, why do I have an error in this sql request

George B92 UPDATE `vjprf_jshopping_manufacturers` SET `meta_title_en-GB`='Buy products from the manufacturer '.`name_en-GB`.' in Ukraine at wholesale prices | Deps' WHERE `manufacturer_id`=3 I need to insert name_en-GBin meta_title_en-GB='some text ( place

How can I justify rows that don't have a specific value?

Travis Bowen: I'm relatively new to SQL and I need to display a row without a specific value in it without adding a value into it's table. The table I have working is: Select * from PERSON; +-------+-------+---------+--------+---------+----------+ | idnum | f

How can I justify rows that don't have a specific value?

Travis Bowen: I'm relatively new to SQL and I need to display a row without a specific value in it without adding a value into it's table. The table I have working is: Select * from PERSON; +-------+-------+---------+--------+---------+----------+ | idnum | f

How can I justify rows that don't have a specific value?

Travis Bowen: I'm relatively new to SQL and I need to display a row without a specific value in it without adding a value into it's table. The table I have working is: Select * from PERSON; +-------+-------+---------+--------+---------+----------+ | idnum | f

How can I justify rows that don't have a specific value?

Travis Bowen: I'm relatively new to SQL and I need to display a row without a specific value in it without adding a value into it's table. The table I have working is: Select * from PERSON; +-------+-------+---------+--------+---------+----------+ | idnum | f

How can I justify rows that don't have a specific value?

Travis Bowen: I'm relatively new to SQL and I need to display a row without a specific value in it without adding a value into it's table. The table I have working is: Select * from PERSON; +-------+-------+---------+--------+---------+----------+ | idnum | f

How can I justify rows that don't have a specific value?

Travis Bowen: I'm relatively new to SQL and I need to display a row without a specific value in it without adding a value into it's table. The table I have working is: Select * from PERSON; +-------+-------+---------+--------+---------+----------+ | idnum | f

I have a class, how do I get to a list view efficiently?

User 2886057 I made a class called Listing: public class Listing { @Key private int id; @Key private String name; @Key private float lastname; public String getName() { return this.name; } public void setName( String name ) { this.name = n

I have a class, how do I get to a list view efficiently?

User 2886057 I made a class called Listing: public class Listing { @Key private int id; @Key private String name; @Key private float lastname; public String getName() { return this.name; } public void setName( String name ) { this.name = n