0

I created a tag like this:

@register.inclusion_tag('post/comment_block.html')
def limit_amount_in_a_page(page_nr=1, post_id=1, amount=5):
    starting_index = page_nr*amount
    for index in range(starting_index, starting_index + amount):
        dosomething()
    has_prev = (page_nr != 0)
    has_next = ((page_nr + 1) * amount) < comments.count()

    return {
        something
    }

The problem is : page_nr is always not an int.

and this is how I call the tag and assign the value to page_nr in the tag:

{% limit_amount_in_a_page page_nr=my_page_nr post_id=post.id amount=4 %}

this is where the value of my_page_nr comes from:

def to_post_page(request, post_id, page_nr):
    post = get_object_or_404(Post, id=post_id)
    form = CommentForm()
    comments = Comment.objects.filter(pk=post_id)
    return render(request, 'post/posts.html', {
        'post': post,
        'form': form,
        'comments': comments,
        'my_page_nr': page_nr,
    })

this is the url calling the view:

url(r'^(?P<post_id>[0-9]+)/(?P<page_nr>[0-9]+)/$', views.to_post_page, name="post"),


{% for post in my_posts %}
            <li><a href="{% url 'forum:post' post.id 0 %} ">{{post.title}}</a></li>
{% endfor %}

The value passed to this url tag should be a int. As you can see, I passed a 0. Really appreciate for any help!

1 Answer 1

1

The value for page_nr is extracted from the URL, and is therefore a string. If you need it to be an int, it's simple to convert it - you could do this in the view, for example:

return render(request, 'post/posts.html', {
    'post': post,
    'form': form,
    'comments': comments,
    'my_page_nr': int(page_nr),
})
Sign up to request clarification or add additional context in comments.

1 Comment

so any thing extracted from url will be a string, right? Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.