Login

Tag "template-tag"

Snippet List

TemplateTag to call a method / function WITH arguments

**Callmethod** - TemplateTag to call a method on an object with arguments from within a template {% callmethod hotel.room_price_for_night night_date="2018-01-02" room_type=room_type_context_var %} ## equals ## >>> hotel.room_price_for_night(night_date="2018-01-02", room_type="standard") #Assuming "standard" is the value of room_type_context_var Django doesn't allow calling a method with arguments in the template to ensure good separation of design and code logic. However, sometimes you will be in situations where it is more maintainable to pass an argument to a method in the template than build an iterable (with the values already resolved) in a view. Furthermore, Django doesn't strictly follow its own ideology: the {% url "url:scheme" arg, kwarg=var %} templatetag readily accepts variables as parameters!! This template tag allows you to call a method on an object, with the specified arguments. Usage: {% callmethod object_var.method_name "arg1_is_a_string" arg2_is_a_var kwarg1="a string" kwarg2=another_contect_variable %} e.g. {% callmethod hotel.room_price_for_night date="2018-01-02" room_type="standard" %} {% callmethod hotel.get_booking_tsandcs "standard" %} NB: If for whatever reason you've ended up with a template context variable with the same name as the method you want to call on your object, you will need to force the template tag to regard that method as a string by putting it in quotes: {# Ensure we call hotel.room_price_for_night() even though there's a template var called {{ room_price_for_night }}! #} {% callmethod hotel."room_price_for_night" date="2018-01-02" room_type="standard" %} * **@author:** Dr Michael J T Brooks * **@version:** 2018-01-05 * **@copyright:** Onley Group 2018 (Onley Technical Consulting Ltd) [http://www.onleygroup.com](http://www.onleygroup.com) * **@license:** MIT (use as you wish, AS IS, no warranty on performance, no liability for losses, please retain the notice) * **@write_code_GET_PAID:** Want to work from home as a Django developer? Earn £30-£50 per hour ($40-$70) depending on experience for helping Onley Group develop its clients' Django-based web apps. E-mail your CV and some sample portfolio code to: [email protected] Copyright 2018 Onley Group Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice, credits, and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

  • template
  • tag
  • templatetag
  • function
  • template-tag
  • args
  • kwargs
  • methods
  • argument
  • template-arguments
Read More

Pretty Print Template Tag

This defines a new template tag that lets you print your rendered templates (or partial templates) in html that has been prettified by beautiful soup. It is dependent on the beautiful soup library (bs4). Not recommended for production but it is helpful for development and serving readable html. {% load whatever_template_file %}' <body> {% pretty %} <h1>Hello, world.</h1> {% endpretty %} </body>

  • beautiful-soup
  • template-tag
  • pretty-print
Read More

Duplicating Template Tag

This template tag will duplicate its contents according to a variable or integer supplied to it. {% duplicate 3 %}a{% endduplicate %} This would return: > aaa

  • template-tag
  • duplicate
  • loop
Read More

Simple template tag to do |stringformat filter with format from a variable

Formats a django variable using a python string formatting as specified in another template variable. Similar to |stringformat. Takes two arguments: the django template variable with the item to be formatted and the django template variable containing the format string. {% pyformat number formatstringvar %} Place this file in `appname/templatetags/pyformat.py` and you're good.

  • template-tag
  • string-formatting
Read More

Yet another query string template tag

This one works works with or without query string dicts defined in the context. And it handles replacement, addition and removal of values for parameters with multiple values. Usage: {% url view %}{% query_string qs tag+tags month=m %} where `view`, `qs` (dict), `tags` (list of strings) and `m` (number) are defined in the context. Full detail in the doc string.

  • url
  • template-tag
  • query-string
Read More

shuffle templatetag

This is useful for randomizing an iterable of objects in place in a django template. Usage: {% load shuffle %} {# now some_list is ordered #} {% shuffle some_list %} {# now some_list is randomized #}

  • template-tag
  • iterable
  • shuffle
Read More

Manipulate URL query strings using context variables using a template tag

A template tag that includes a modified version of the GET query string. the query string can be manipulated by adding and removing fields. If a value is given that resolves to a context variable that the value of the variable is used. Based on [this snippet by dnordberg](http://djangosnippets.org/snippets/826/), but with the ability to use context and done in a cleaner manner, without the need to add an arbitrary template.

  • url
  • template-tag
  • query-string
Read More

Template tag which gets specific GET variables from the current request

This template tag attempts to get specific GET variables from request.GET. If such variables exist, it shows them as a query string (with optional "include_ampersand" mode when it puts an "&" at the end if there is a result string, or a "?" if there's none: it is used when you need to add a new variable to the query string) or as hidden input fields ("html_form" mode).

  • get
  • template-tag
  • request
  • variables
  • get-variables
Read More

favicon Template Tag

A simple template tag that returns the favicon URL for a given arbitrary URL. Put the code into a python module in the templatetags package of a Django app (e.g. myapp/templatetags/mytags.py), and use it like this: {% load mytags %} ... <img src="{% favicon posting.url %}/> <a href="{{ posting.url }}">{{posting.title}}</a> ...

  • templatetag
  • template-tag
  • favicon
Read More

Flatpage Suggester Template tag for 404 templates

This template tag finds FlatPages with urls 'similar' to the given request_path. It takes the request_path from the page_not_found view (django.views.defaults), picks it apart, and attempts to match existing FlatPages that have a 'similar' URL. For example, if the URL that resulted in a 404 was: /foo/bar/baz/whatever/ This tag would look for FlatPages whose URL starts with the following: /foo/bar/baz/whatever/ /foo/bar/baz/ /foo/bar/ /foo/

  • template-tag
  • flatpage
  • 404
  • suggestion
Read More

Template Tag to protect the E-mail address

This is a Django Template Tag to protect the E-mail address you publish on your website against bots or spiders that index or harvest E-mail. It uses a substitution cipher with a different key for every page load. It basically produce a equivalent Javascript code for an email address. This code when executed by browser make it mailto link. **Usage** `{% encrypt_email user.email %}` **More Info ** [Template Tag to protect the E-mail address](http://www.nitinh.com/2010/02/django-template-tag-to-protect-the-e-mail-address/)

  • template
  • django
  • javascript
  • email
  • template-tag
  • obfuscate
  • bots
  • spider
Read More

Digg-like pagination

My take on digg-like pagination. Save the code as 'templatetags/pagination_nav.py' in one of your apps. It relies on a 'pagination_nav.html' template. Here is a base template: {% if pages %} <div class="bottom-pagination-nav"> {% if previous_url %}<a href="{{ previous_url }}">{% else %}<span>{% endif %}&laquo; Previous{% if previous_url %}</a>{% else %}</span>{% endif %} {% for group in pages %} {% for page in group %} {% if page.current %}<span>{{ page.number }}</span>{% else %}<a href="{{ page.url }}">{{ page.number }}</a>{% endif %} {% endfor %} {% if not forloop.last %}<span>...</span>{% endif %} {% endfor %} {% if next_url %}<a href="{{ next_url }}">{% else %}<span>{% endif %}Next &raquo;{% if next_url %}</a>{% else %}</span>{% endif %} </div> {% endif %}

  • pagination
  • template-tag
  • paginator
  • digg
Read More

30 snippets posted so far.