The template API for view objects.
The view object has the following things on it:
entries: A list of all of the entries that are visible and authorized in this viewOptionally takes a parameter,
unauthorized, which indicates the maximum number of unauthorized entries which will be included. If this is a number, that number of unauthorized entries will be included; if True, all unauthorized entries will be included. These entries will count against thecountlimit, if any. Defaults to0.Iterating on the
viewitself is equivalent to iterating on itsentrieswithout any arguments.deleted: A list of all of the entries that were deleted from this view (withStatus: GONEorStatus: DELETED): A list of entries which would be included if the user were authorized.
This is mostly so that a template can indicate whether a logged-out user should try logging in to see the protected entries, or for feeds to show placeholders.
Optionally takes a parameter,
count, which indicates the maximum number that will be included. If not specified it will use thecountlimit, if any.Note that visible, authorized entries do not count against the limit.
: Indicates whether the view has any entries which could become visible if a different user were logged in.
count: The number of visible entries in the viewlast_modified: A last-modified time for this view (useful for feeds)spec: The view’s specification (category, count, date, etc.)This is in the form of the arguments that would be passed to
get_viewto obtain this view. For example, you can use'date' in view.specto determine whether a view is date-based.previous: The previous page for this view, based on the current sort ordernext: The next page for this view, based on the current sort orderolder: The previous page for this view, going back in timenewer: The next page for this view, going forward in timecurrent: The current/default page of this view; can take additional restriction parametersis_current: Whether this view is the current/default pagepaging: Returns the type of pagination that is in effect for this view; one of:offset: Based on entry offsetyear: Based on the entry’s publication yearmonth: Based on the entry’s publication monthday: Based on the entry’s publication day
These values are the same as the
pagingparameter ofentry.archive.pages: A list of every page for this view based on the current pagination.Note: This will probably be very slow; use sparingly.
link: The link to this view; optionally takes the following arguments:category: Which category to use (defaults to the category this view is queried against)template: Which template to use (defaults to the index template)absolute: Whether the URL should be absolute or relativeFalse: Use a relative URL (default)True: Use an absolute URL
first: The first entry in the viewlast: The last entry in the viewnewest: The newest entry in the viewoldest: The oldest entry in the viewrange: A textual description of the range of entries on this view, if the view has any sort of pagination-type constraints. Takes the following optional arguments:day: The format to use for daily pagination, or a time period within a month; defaults toYYYY/MM/DDweek: The format to use for a weekly pagination; defaults toYYYY/MM/DDmonth: The format to use for a monthly pagination, or a time period covering multiple months; defaults toYYYY/MMyear: The format to use for a yearly pagination, or a tim eperiod covering multiple years; defaults toYYYY/MMspan: The format to use for a range of entries where the first and last entry dates differ (after formatting); defaults to{oldest} — {newest} ({count})single: The format to use for a range of entries where the first and last are within the same time period; defaults to{oldest} ({count})
If there is only a single entry in the view, the
rangeproperty will be formatted for its time period directly, and it only uses theday,month, oryearformat.If there are multiple entries, then the time period is chosen with the following logic:
- If they are within the same month, it uses
day - If they are within the same year (but on different months), it uses
month - Otherwise, it uses
year
Both dates are formatted based on the format corresponding to the chosen period. If they are the same, the
singleformat is used; otherwise thespanformat is used. Both of these formats receive the following template values:oldest: The formatted date of the earliest entrynewest: The formatted date of the latest entrycount: The number of entries in the view
The specified formats for
daymonthandyearcan be as specific as you would like; for example, if you want to always show the months in question regardless of the span, you can set all three formats toYYYY-MM, or on the other end of the spectrum you could indicate the full format down to the nanosecond if you really want to for some reason.Any format string accepted by Arrow is acceptable (for example,
MMMM YYYYwill appear asJanuary 2012).tag_add: Generates a view with one or more tags addedtag_remove: Generates a view with one or more tags removedtag_toggle: Generates a view with one or more tags toggledThis is useful for a tag browser where you want to be able to toggle tags on and off; for example:
<ul> {% for name,count in category.tags %} <li><a href="{{view.tag_toggle(name)}}">{{name}}</a> ({{count}} entries)</li> {% endfor %} </ul>
Getting subviews
Any view object can also take arguments to further refine the view; the following arguments are supported:
category: The top-level categories to consider; can be a single category or a list of categories.Setting this to
Nonewill get entries from all categories; this is equivalent tocategory='',recurse=Truecategory_not: Categories to exclude; this can be either a single category, or a list of categories.recurse: Whether to include subcategories.True: Include subcategoriesFalse: Do not include subcategories (default)
future: Whether to include entries from the future.True: Include future entriesFalse: Do not include future entries (default)
date: Limit to entries based on a specified date; this can be of the formatYYYY,YYYY-MM, orYYYY-MM-DD.count: Limit to a maximum number of entries.If
dateis set,counthas no effect.entry_type: Limit to entries with a specificEntry-Typeheaderentry_type_not: Limit to entries which do NOT match a specific entry typeThese can be a single string, or it can be an array of strings. Note that these are case-sensitive (i.e.
"PaGe"and"pAgE"are two different types).get_view(entry_type='page'): only get entries of type “page”get_view(entry_type_not='page'): only get entries which AREN’T of type “page”get_view(entry_type=['news','comic']): get entries which are of type ‘news’ or ‘comic’get_view(entry_type_not=['news','comic']): get entries of all types except ‘news’ or ‘comic’
Mixing
entry_typeandentry_type_notresults in undefined behavior, not that it makes any sense to do that anyway.tag: Limit the view to entries which match the listed tag(s).tag_filter: What filter criterion to use for matching tags. One of:'ANY': All entries which have at least one of the listed tags (default); this can also be spelled'OR''ALL': Entries which have all of the listed tags; this can also be spelled'AND''NONE': Entries which have none of the listed tags'; this can also be spelled'NOT'
start: Limit the view to start with this entry, regardless of sort orderlast: Limit the view such to none newer than the specified entry (by id or object)first: Limit the view such to none older than the specified entrybefore: Limit the view to only entries which came before the specified entryafter: Limit the view to only entries which came after the specified entryorder: What sort order to provide the entries in; one of:oldest: Oldest-firstnewest: Newest-first (default)title: Sorted alphabetically by title
has_attachments: Limit the view to entries with (ifTrue) or without (ifFalse) attachmentsis_attached: Limit the view to entries which are (ifTrue) or are not (ifFalse) attachmentsattachments: Limit the view to entries which are attachments of the specified entryattached: Limit the view to entries which have the specified entry attached
For example, this will print the titles of the first 10 entries in the view, including subcategories:
{% for entry in view(count=10,recurse=True) %} {{entry.title}} {% endfor %}
If you want to use the same view refinement multiple times, you can use {% set %}:
{% set content = view(count=10,recurse=True,entry_type_not='sidebar') %} {% if content.entries %} <ul> {% for entry in content.entries %} <li>{{entry.title}}</li> {% endfor %} </ul> {% endif %}
view(...) vs get_view(...)
The global get_view() function creates an entirely new view from scratch, rather
than basing it on the current default view. Some possible uses for this are:
- Getting a link to the latest entry on the entire site (e.g.
get_view().newest) Properly showing the current category’s sidebar links without filtering them by date or page (e.g.
get_view(category=category,entry_type='sidebar'))Note that this can also be achieved with
view.current(entry_type='sidebar')Having a global set of sidebar links (e.g.
get_view(entry_type='sidebar'))