Login

Tag "template"

Snippet List

Yet Another Image Resizer

There's a whole range of examples online for resizing images in Django some of which are incredibly comprehensive with a wide variety of options. Here's my take on the task that serves as a simple drop in for when you don't want to include a separate app. - Only generates the resized image when first requested. - Handles maintaining proportions when specifying only a width or a height. - Makes use of PIL.ImageOps.fit for cropping without reinventing the wheel.

  • template
  • tag
  • image
  • pil
  • thumbnail
  • resize
Read More

Multipart Templated Email

A replacement for sending mail that takes the name of a template which exists in both text and html formats, and uses these to create a multipart email populated with the given context.

  • template
  • email
Read More

Formset Form

Template designers often require access to individual form fields or sets of form fields to control complex form layouts. While this is possible via looping through form fields in the template it can lead to ugly logic inside templates as well as losing the ability to use the as_* form rendering methods. This class when mixed into a form class provides hooks for template designers to access individual fields or sets of fields based on various criteria such as field name prefix or fields appearing before or after certain fields in the form, while still being able to use the as_* form rendering methods against these fields.

  • template
  • forms
  • mixin
Read More

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

DRY template rendering decorator

in this case the 'render_template' decorator assumes there is a myview.html template. this keeps things simple and you DRY. Hope it helps. Regards, Paul

  • template
  • rendering
  • dry
Read More

View decorator to automate templates.

Place the above code in your view, and then you can use it to specify what template to use. Set elements of the context as a dictionary and that gets passed to the template as well. For example: In view.py: #################################### @with_template('friends/index.html') def friends(request, context, username): context['user'] = User.objects.get(username = username) in friends/index.html: {% extends "base.html" %} {% block content %} <h1>{{ user.username }}'s Friends</h1>

  • template
  • view
  • decorator
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

Template range loop

This tag is meant to mimic the python use of range in a for-loop: 'for i in range(start, end, step)'. It is implemented like a loop and it takes both variable names from the context and constant integers as arguments. Syntax: {% range end as i %} {{ i }} {% endrange %} {% range start:end as i %} {{ i }} {% endrange %} {% range start:step:end as i %} {{ i }} {% endrange %}

  • template
  • range
  • loop
Read More
Author: nfg
  • 0
  • 0

Fieldsets for Views

This Snippet allows a view to controle the printed forms on the templates, in a similar way to the fieldsets used by the django admin. How to Use: In the view in question, put: def some_view(request): ... fieldsets = ( (u'Title 1', {'hidden' : ('field_1', 'field_2',), 'fields' : ('field_3',)}), (u'Title 2', {'hidden' : ('field_5', 'field_6',), 'fields' : ('field_4',)}),) ) return render_to_response('some.html', {'fieldsets': fieldsets}) fieldsets = ( (None, {'hidden' : ('evento', 'colaborador',), 'fields' : ('acompanhantes',)}), ) Next, in the html just add: <form enctype="multipart/form-data" id="edit" method="post" ...> ... {% include "inc/form_snippet.html" %} ... <input type="submit" value="Submit"> </form>

  • template
  • django
  • admin
  • views
  • filters
  • python
  • tags
  • html
  • css
  • dicts
Read More
Author: Nad
  • 2
  • 0

Admin Apps Names Translation

This Snippet allows for your project's apps names to be displayed as you want in the Admin, including translations. The lists of apps and languages are created from your settings.py file. **How to use** 1st part: - Create a application called 'apps_verbose' in you project with the models.py and admin.py showed here - Create a folder inside it with named 'templatetags' with the verbose_tags.py file inside it. - Add this app to installed apps in your settings.py - If you change this app name, dont forget to correct the imports. 2nd part: - Create a folder named 'admin' in your templates folder and copy the following files form your /django/contrib/admin/templates/admin/ folder. - /app_index.html - /base.html - /change_form.html - /change_list.html - /delete_confirmation.html - /delete_selected_confirmation.html - /index.html - /object_history.html - Make the necessary changes in each file, like shown here. 3rd part: - Create translations in the Admin and enjoy.

  • template
  • django
  • admin
  • i18n
  • python
  • tags
  • html
  • app
  • translation
Read More
Author: Nad
  • 3
  • 5

jinja2 csrf_token extension

init env `env = Envoriment(extensions=('youproject.app.extensions.csrf_token'), loader=loader)` or see [http://www.djangosnippets.org/snippets/1844/] and in settings.py: `JINJA_EXTS=('jinja2.ext.i18n','youproject.app.extensions.csrf_token',)` use this extension in jinja2 template just like django template: `<form ...>{% csrf_token %}...</form>`

  • template
  • jinja2
  • csrf
Read More

integrated jinja2 which could use generic view ,my djangojinja2.py

I tried a few snippets of integrated jinja2 in django, which provided ?.render_to_string and ?.render_to_response in the way of jinja2. **But those snippets could not use the generic view**, because of the generic views is use default django template. so i write this snippet which could use generic view, and use the basic django.shortcuts.render_to_string, django.shortcuts.render_to_string. #in yourproject/urls.py : #install default environment is very simple djangojinja2.install() #install environment of specified Environment class from jinja2.sandbox import SandboxedEnvironment djangojinja2.install(SandboxedEnvironment) #alternative you can install sepcified environment env=Environment(...) ... djangojinja2.install(env) #something could be set in settings.py TEMPLATE_DIRS = (path.join(path.dirname(__file__),"templates"),) JINJA_GLOBALS=['myapp.myutil.foo',] JINJA_FILTERS=['django.template.defaultfilters.date',] JINJA_TESTS=('foo.mytest',) JINJA_EXTS=['jinja2.ext.i18n'] #there is no change need for app.views from django.shortcuts import render_to_response def foo(request): return render_to_response('/myjinja2.html',{'request':request}) test in django development version of r12026 , jinja2 2.2.1, python 2.5

  • template
  • generic
  • jinja2
  • generic-view
Read More

Currency formatting filter

See the function **docstring** for usage. This template filter has a couple of drawbacks: * Uses **locale.setlocale** which, according to the [Python docs](http://docs.python.org/library/locale.html#locale.setlocale), is not thread safe. I don't know how this may affect Django applications. * Requires Python 2.5+. Updated 2011-03-16.

  • template
  • filter
  • currency
  • formatting
Read More

262 snippets posted so far.