Django Endless Pagination - Multiple paginations in the same page - python

My Django version is 1.10.5
With Django Endless Pagination 3.0.1 i want to show two or more paginated lists(see code) on my site.
For that i try to do this tutorial but the solution raises everytime an error if i use a second variable or multiple variables. The second variable(the querystring) in the tutorial is "other_entries_page".
The answer from the testserver: Invalid arguments for u'paginate' tag
I have no idea whats going wrong.
Here is the code from the tutorial:
{% load el_pagination_tags %}
{% paginate entries %}
{% for entry in entries %}
{# your code to show the entry #}
{% endfor %}
{% show_pages %}
{# "other_entries_page" is the new querystring key #}
{% paginate other_entries using "other_entries_page" %}
{% for entry in other_entries %}
{# your code to show the entry #}
{% endfor %}
{% show_pages %}
The variables that i mean are GET-variables(like: http://example.com/?page=2).
I hope you can help me.

Related

How to access(loop through) queryset passed from views to html template inside javascript block in django?

I want to loop through array/queryset passed from view to html template in django inside
{% block js %}
block, is it possible ?
{% block js %}
I want to access array/queryset/value passed from views here
{% endblock %}
Yeah, You can do it Like this,
{% for item in array %}
{‌{ item }}
<br>
{% endfor %}

How to check existence of an object using pk in django template?

I tried this if condition to show previous and next posts only if it exists in database.
{% if blog.pk|add:-1 %}
Previous Post
{% endif %}
{% if blog.pk|add:1 %}
Next Post
{% endif %}
but, it kinda ignores the condition and always show next and previous posts buttons owing to the fact that this condition is wrong. How do I fix this?
Please, don't implement this in the template. Templates should be used for rendering logic, not business logic.
You can define a method for example on your Post object to obtain the previous and next item:
class Blog(models.Model):
# …
def get_next(self):
return Blog.objects.filter(pk__gt=self.pk).order_by('pk').first()
def get_previous(self):
return Blog.objects.filter(pk__lt=self.pk).order_by('-pk').first()
Then you thus can use these methods:
{% with prev_blog=blog.get_previous %}
{% if prev_blog %}
Previous Post
{% endif %}
{% endwith %}
{% with next_blog=blog.get_next %}
{% if next_blog %}
Next Post
{% endif %}
{% endwith %}
This will also fetch Blog objects if there is a "gap". So if there is no Blog object with pk=4, then the get_previous of a Blog with pk=5, will be a Blog with pk=3.

django 1.8.3 tutorial index.html template wrong to get corrent url

I'm learning Django 1.8.3 with tutorial and I came here: Removing hardcoded URLs in templates. I followed this section and modified the polls/template/polls/index.html like this:
{% if latest_question_list %}
<ul>
{% for question in latest_question_list %}
<!--<li>{{question.question_text}}</li>-->
<li>{{ question.question_text }}</li>
{% endfor %}
</ul>
{% else %}
<p> No polls are available. </p>
{% endif %}
But when I access Question by clicking the URL I get 404 like this: 404 page when click a question.
I don't know what's wrong with my code?
Your Django template tag ends with % } (on the error page that you link to), and therefore Django doesn't recognize it.
Remove the space.

Django flatpages template tag doesn't work with template inheritance

I have a Django (1.6) application that inherits a base template. I would like to include one of my (currently working) flatpages into the application landing page, something that the Django docs say is possible.
Here is my template:
{% extends "path/to/base.html" %}
{% load flatpages %}
{% get_flatpages as fp %}
{% block content %}
<h3>Flatpage inclusion</h3>
<p>Number of flatpages: {{ fp|length }}
<ul>
{% for page in fp %}
<li>{{ page.title }}</li>
{% endfor %}
</ul>
{% endblock content %}
This does not list any of the flatpages. However, if I remove the {% extends %} signal, so my code looks like this:
{% load flatpages %}
{% get_flatpages as fp %}
<h3>Flatpage inclusion</h3>
<p>Number of flatpages: {{ fp|length }}
<ul>
{% for page in fp %}
<li>{{ page.title }}</li>
{% endfor %}
</ul>
Everything works. I see the number of flatpages in my fp object (9) and my unordered list shows all the flatpage urls and titles.
This seems to me to be a bug in either how flatpages work, or how Django does template inheritance.
The base template (/path/to/base.html) doesn't have anything complex in it.
Django categorically says that this is possible:
When you load a custom tag or filter library, the tags/filters are only made available
to the current template – not any parent or child templates along the template-inheritance path.
For example, if a template foo.html has {% load humanize %}, a child template (e.g., one that
has {% extends "foo.html" %}) will not have access to the humanize template tags and filters.
The child template is responsible for its own {% load humanize %}.
This is a feature for the sake of maintainability and sanity.
Has anyone else noticed this bug? Is it an exception for just the built-in flatpages app?
EDIT 1:
Daniels answer is correct. The example code from the Django docs doesn't show including flatpage content within a {% block %}, so I didn't expect that it needed to be done:
{% load flatpages %}
{% get_flatpages as flatpages %}
<ul>
{% for page in flatpages %}
<li>{{ page.title }}</li>
{% endfor %}
</ul>
My fault I guess. Live and learn.
The problem is that your get_flatpages tag is outside any blocks from the parent template. That means it simply won't be called.
Move it into the content block and it should work.
Just to reiterate the correct solution- get_flatpages needs to be placed inside the block where it's going to be referenced. So this will work:
{% extends "index.html" %}
{% load flatpages %}
{% block footer %}
{% get_flatpages as flatpages %}
{% for page in flatpages %}
...
{% endfor %}
And this will not work:
{% extends "index.html" %}
{% load flatpages %}
{% get_flatpages as flatpages %}
{% block footer %}
{% for page in flatpages %}
...
{% endfor %}
And yes, Django documentation isn't very clear on that.

Django CMS page_attribute falls back to other values?

I'm currently editing the base template that all my Django-CMS templates inherit from. What I'm trying to do is printing the page_title attribute, and if that hasn't been set, print {{ block.super }} instead.
My current code is this:
{% block title %}
{% page_attribute "page_title" as cms_title %}
{% if cms_title and cms_title.strip %}
{{ cms_title }}
{% else %}
{{ block.super }}
{% endif %}
{% endblock %}
Now, when the page title property is set, this works as expected. However, when the page title is left blank, the page's title attribute is used instead which I wouldn't expect.
Is this expected behaviour? If it is, how can I work around this? Or is there another approach to get the result I want?
I'm using Django-CMS 3.0
Edit
I've been told that this is expected behaviour, but no workaround was provided. The solution that was good enough for my situation was to compare the page_title and title attributes. If they're equal, the title is probably being used as a fallback so I could assume the page_title wasn't set.
If anyone has a better suggestion, feel free to answer :)
I've just given this a go & you can conditionally override the block like so;
{% extends 'base.html' %}
{% load i18n cms_tags %}
{% page_attribute "page_title" as cms_title %}
{% if cms_title %}
{% block title %}
{{ cms_title }}
{% endblock %}
{% endif %}

Categories