Login

mini_render_to_response

Author:
menendez
Posted:
April 19, 2009
Language:
Python
Version:
1.0
Score:
0 (after 0 ratings)

You need the mini-detector middleware installed http://www.iterasi.net/openviewer.aspx?sqrlitid=-e2dfig9w0yrclxaigp-uw.

This is a drop in replacement to render_to_response. When using mini_render_to_response it will try to load a version of your template with mini at the end. For example "home_mini.html" instead of "home.html". If it doesn't find the _mini version it falls back to the regular "home.html" version of your template.

Easy way to maintain a "small screen" version of your templates for iPhone or other small screen devices.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def mini_render_to_response(template, context, context_instance, try_mobile = True, *args, **kwargs):
    render = None
    if try_mobile and context_instance['request'].GET.has_key('mini'):
        try:
            file, ext = template.split('.')
        except ValueError: pass
        else:
            mini_template = file + '_mini.' + ext
            try:
                render = render_to_response(mini_template, context, context_instance=context_instance, *args, **kwargs)
            except TemplateDoesNotExist: pass
    
    if not render:
        render = render_to_response(template, context, context_instance=context_instance, *args, **kwargs)
    
    return render

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.