Login

Tag "tag"

Snippet List

Template Tag Render Decorator

I often find myself needing to create a template tag and all I'm interested in is the token and the render function. This decorator abstracts away the boilerplate code around creating the template node class which is the same each and every time.

  • template
  • tag
  • decorator
Read More

custom template tag sample

usage: <div class="content_box" id="tests" style="background:#cfc"> {% load extra %} <p>{% get_current_time '%Y-%m-%d %I:%M %p' as the_current_time %}/{{ the_current_time }}</p> {% upper %} <p>this is a test.</p> {% endupper %} <p>{% a_current_time '%Y-%m-%d %I:%M %p' %}</p> {% post_for_member member %} </div>

  • tag
Read More

Get sorted items for any model in any order

This get_sorted_items tag takes app.model, a number n, a a field name to sort ,and a variable-name as arguments and will deliver the n first objects of the model. it checks if a Manager *publicmgr* exists and calls this if the user isn't authenticated. Additionally it write the count of the model to the context

  • template
  • tag
  • dry
Read More

Render arbitrary models - template tag

This template tag provides an easy way to render objects in your template, even if you don't know ahead of time what type they are. For example, if you've got a search page with a result list comprised of objects from various models, you can simply loop through them and render them using the tag. The tag will choose the best template and render the object for you. The tag's docstring has all the details. I hope you find this as useful as I have. Questions, comments, complaints welcome.

  • template
  • tag
  • model
  • render
  • display
Read More

simple tag-cloud Template Tag

This Template Tag computes the font-size of two given arguments and returns a CSS-encoded string like "font-size: XXpx;", which can be used to format the font-size of link. (The minium font-size must be set with CSS.) Requires two arguments: 1. occurrence of the current tag 2. sum of all occurrences It works great for my tag-cloud. [Source of the logarithmic formula](http://www.php.de/php-fortgeschrittene/44928-tag-cloud-algorithmus-fuer-schriftgroessye.html) **Usage** `<a href="http://www.anything.com" {% cloudify variable.occurrence overall. occurrence_sum %} title="anything">any tag</a>`

  • tag
  • cloud
  • logarithmic
Read More

CharField powered Tags with ChoiceField widget.

Shell example: >>> pprint.pprint(settings.FORUM_TAGS) ((u'x11', 'Xorg'), (u'pacman', 'Pacman'), (u'aur', 'AUR'), (u'abs', 'ABS'), (u'howto', 'HOWTO'), (u'instalacja', 'Instalacja'), (u'offtopic', 'Offtopic')) >>> t = Thread.objects.all()[0] >>> pprint.pprint(t.tags) [{'slug': u'pacman', 'title': 'Pacman'}, {'slug': u'abs', 'title': 'ABS'}, {'slug': u'howto', 'title': 'HOWTO'}, {'slug': u'instalacja', 'title': 'Instalacja'}, {'slug': u'offtopic', 'title': 'Offtopic'}] >>> t.tags = [{'slug': 'abs'}, {'slug': 'howto'} >>> t.save() >>> t = Thread.objects.get(pk=t.pk) >>> pprint.pprint(t.tags) [{'slug': u'abs', 'title': 'ABS'}, {'slug': u'howto', 'title': 'HOWTO'}] >>> t.tags = ['Offtopic', 'HOWTO'] >>> t.save() >>> t = Thread.objects.get(pk=t.pk) >>> pprint.pprint(t.tags) [{'slug': u'howto', 'title': 'HOWTO'}, {'slug': u'offtopic', 'title': 'Offtopic'}]

  • tag
  • forms
  • field
  • selectmultiple
  • checbox
Read More

Profanity Check

Simple Python snippet to detect if any word in a list of words is inside your string. Use for profanity checking (my use case), auto tag detection, scoring, etc. This will return an empty list if the word is not in the list. Assumes everything in words_to_find is lower case. Can probably be done cleaner with regular expressions but this method is extremely readable for those that prefer none regex solutions.

  • tag
  • profanity
Read More

improved sortby template tag

Variation on dictsort using attribute access. Nested attributes can be used, like, "obj.attr.attr_attr" Example usage: {% for entry in entries|sortby:'category.title' %} Based on [1609](http://www.djangosnippets.org/snippets/1609/)

  • template
  • tag
  • dictsort
Read More

Template tag to create mailto links with options

A {% mailto %}{% endmailto %} template tag that requires an e-mail destination and optionally accepts subject, cc and bcc. It will then wrap whatever is within the tag in an `<a href="mailto:..."> </a>` HTML tag. See the docstring in the code for the {% mailto %} usage and some examples. You will need to load this template tag to your template. You can find detailed instructions [here](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#code-layout). But in a nutshell: 1. Create a templatetags package (meaning a directory with a __init__.py file in it) on the same level as your application's model.py 2. Put the code for this tag in a module (example: extra_tags.py) 3. On your template use {% load extra_tags %} -- note: the app where the templatetags package was created needs to be in INSTALLED_APPS 4. Use {% mailto user.email 'You subject here for {{user.get_full_name}}' %}blah{% endmailto %} This is my first django template tag. I am also NOT tremendously experienced with Python. Criticism and corrections are more than welcome.

  • template
  • tag
  • templatetag
  • email
  • mailto
Read More

User groups template tag

This tag builds on top of the [ifusergroup/else tag](http://www.djangosnippets.org/snippets/390/), fixes a small bug and introduces support for else blocks. This adds a way to provide multiple groups via group1|group2|group3

  • tag
  • templatetag
  • user
  • auth
  • group
Read More

A templatetag to insert the output of another view (or local URL)

Inserts the output of a view, using fully qualified view name (and then some args), a or local Django URL. {% view view_or_url arg[ arg2] k=v [k2=v2...] %} This might be helpful if you are trying to do 'on-server' AJAX of page panels. Most browsers can call back to the server to get panels of content asynchonously, whilst others (such as mobiles that don't support AJAX very well) can have a template that embeds the output of the URL synchronously into the main page. Yay! Go the mobile web! Follow standard templatetag instructions for installing. **IMPORTANT**: the calling template must receive a context variable called 'request' containing the original HttpRequest. This means you should be OK with permissions and other session state. **ALSO NOTE**: that middleware is not invoked on this 'inner' view. Example usage... Using a view name (or something that evaluates to a view name): {% view "mymodule.views.inner" "value" %} {% view "mymodule.views.inner" keyword="value" %} {% view "mymodule.views.inner" arg_expr %} {% view "mymodule.views.inner" keyword=arg_expr %} {% view view_expr "value" %} {% view view_expr keyword="value" %} {% view view_expr arg_expr %} {% view view_expr keyword=arg_expr %} Using a URL (or something that evaluates to a URL): {% view "/inner" %} {% view url_expr %} (Note that every argument will be evaluated against context except for the names of any keyword arguments. If you're warped enough to need evaluated keyword names, then you're probably smart enough to add this yourself!)

  • template
  • ajax
  • tag
  • templatetag
  • view
  • httprequest
  • mobile
  • include
Read More

Sometimes Tag

Adds a templatetag that works like an if block, but . The one and only argument is a float that reflects the percentage chance. It defaults to .2, %20. {% sometimes %} <img src='spy_behind_sniper.jpg'/> {% else %} <img src='sniper.jpg'/> {% endsometimes %} -- or -- {% sometimes .001 %} You win! {% else %} Sorry, not a winner. Play again! {% endsometimes %} -- or -- {% sometimes .5 %} This shows up half the time. {% endsometimes %}

  • tag
  • random
Read More

partial tag

This is another partial tag, taken from a previous partial tag. The previous one assumed template locations by hardcoding in "partials/%s", etc. I took all that out, and made it work. And I took out the not needed third parameter for passing in your own data So now you call like this: {% partial "partials/mypartial.html" %} It passes the template context var into the partial, so anything you do in the main template, will work in the partial Just make sure you've got all the right imports.

  • tag
  • python
  • partial
Read More

split_contents2 for template tags

Is an updated way of splitting contents for a token, it does the split, but fixes the list.. EX: From a tag call like this: {% partial "partials/template.html" %} usually you get: ['partial','"partials/template.html"'] notice the " double quotes fixes it with: ['partial','partials/template.html'] takes out the " quotes

  • template
  • tag
  • python
Read More

119 snippets posted so far.