Login

Most bookmarked snippets

Snippet List

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

Testrunner with testmodels

I miss the ability to use testmodels in app-tests. Using this testrunner you can include a test "app" in the app's tests module. Say if you have a module test_app.models within the tests module you could use it in the test like this: from django.test import TestCase from some_django_app.tests.test_app import models as testingmodels TEST_APPS = 'test_app' class ATestCase(TestCase): def test_no_url_model_signals(self): thing = testingmodels.ThingModel(name=u'a small thing') `TEST_APPS` can either be a string, a list or a tuple. This testrunner works with Django 1.1. It is based on the simple testrunner included with Django.

  • models
  • testrunner
Read More
Author: nfg
  • 0
  • 0

admin.py generator from a model

Generates admin.py file for a models.py file with the basic structure that could be edited. Usage: python gen_admin_py.py path/to/models.py creates a file admin.py in the same directory as models.py. import your models as you edit the admin.py

  • admin
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

Amazon product-data interface class for Django-friendly PyAWS queries

I am not sure what to say about the state of PyAWS, or its future, what with the multiple forks available and lack of recent updates. The best version I've found is [http://github.com/IanLewis/pyaws](this one), a spiffed-up version of 0.2.2 by Ian Lewis. I wrote this class on top of PyAWS so I could have more pythonic/django-y calling conventions, and to isolate the calls in case I have to swap libraries or versions down the road. You may want to familiarize yourself with PyAWS before using this. You'll definately need Amazon web service login credentials and keys -- they're available [here](http://aws.amazon.com/) for free. personally I use it with [these monkeypatching and decorator-decorators](http://www.djangosnippets.org/snippets/1888/) -- at the top of my personal version of the file containing this snippet I use the two (non-silly) examples from that snippet, to make the PyAWS internal Bag collection class work for me. EXAMPLE USE: # search Amazon's product database (returns a list of nested dicts) from amazon import aws books = aws.search(q='raymond carver') lenses = aws.search(q='leica summicron', idx='Photo') # get the data for a specific ASIN/ISBN/EAN/etc ID number what_we_talk_about_when_we_talk_about_love = aws.fetch(qid='0679723056', idtype='ASIN')

  • decorator
  • amazon
  • amazonapi
  • aws
  • pyaws
Read More

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

HibernateBooleanField

Sometimes its necessary to map your django models to Java Hibernate created tables. Hibernate maps boolean field to bit(1) column instead of tinyint in django. NOTE: tested for mysql backend only

  • model
  • field
  • hibername
Read More

Username filled automatically with id

I thought this code for insert automatically id in username field. This method should be used in save method. This code work on a dbms that support transactions ( example: mysql+innodb or postgresql ). Let me know what you think about this snippet and if you advice an alternative solution by commenting below. Thanks :)

  • username
  • id
  • transaction
Read More

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

simplified render_to_response with RequestContext

manything need to do with RequestContext, but it's too tedious. use `render_to_response("/my.html", {'key':value,},request)` instead of `render_to_response("/my.html", {'key':value,},new RequestContext(request)) ` and you can also use `render_to_response("/my.html", {'key':value,},new RequestContext(request))`

  • shortcuts
  • simplified
Read More

Make hyperlinks for labels of raw_id_fields (jQuery)

The Django Admin site provides a "raw ID" feature for foreign keys that reference large tables. In the form view, the label of the referenced object will appear after the input. This snippet will make that label into a hyperlink that takes you to the form view for that object. To use this snippet, copy base_site.html into your templates/admin directory (if you haven't already), and paste this code into that file somewhere after the 'extends "admin/base.html"' directive. If you are already using jQuery, you can remove the first script tag. If you are already extending "extrahead", just paste the script tag(s) into the block you have created. [Documentation for raw_id_fields](http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.raw_id_fields)

  • admin
  • jquery
  • raw_id_fields
Read More

Encoding datetime for JSON consumers like YUI

Passing datetimes from Python to a [YUI DataTable](http://developer.yahoo.com/yui/datatable/) via JSON served by [django-piston](http://bitbucket.org/jespern/django-piston/) turned out to be surprisingly rocky. This code actually works with ``YAHOO.lang.JSON.stringToDate`` (*not* ``YAHOO.util.DataSource.parseDate``) which cannot handle time zone specifiers other than "Z" or dates without timezones. The YUI [DataSource](http://developer.yahoo.com/yui/datasource/) which uses this looks something like this - note that simply setting ``format:"date"`` does not work as that uses ``YAHOO.util.DataSource.parseDate``, which uses``Date.parse`` to do the actual conversion, which will involve browser-specific formats and as of this writing only Chrome's native ``Date`` can reliably parse ISO 8601 dates. myDataSource.responseSchema = { resultsList: '…', fields: [ … { key: 'my_date_field', parser: YAHOO.lang.JSON.stringToDate }, ], … };

  • javascript
  • date
  • json
  • iso8601
Read More

3110 snippets posted so far.