Login

Snippets by davmuz

Snippet List

Admin page without inlines dinamically

Sometimes the related objects needs that the main object exists in order to edit and save them properly. There are two main solutions: override the ModelAdmin.add_view() view or remove the inlines only from the add view page (and not from the change page). The former requires a lot of coding, the latter it's impossible without patching Django because the inlines are not dynamic. **This simple solution hides the inline formsets only from the add page, and not from the change page.** Adding an "if" structure it is possible to choose the inlines to use. Example use case: when a related inline model have to save a file to a path that needs the ID key of the main model, this solution prevent the user to use the related inline model until the model it's saved. Tested on Django-1.4, should work since Django-1.2.

  • admin
  • inlines
  • change_view
  • add_view
Read More

Precise truncate chars filter

Template filter that truncates the text when it exceeds a certain number of characters. It deletes the last word only if partial. Adds '...' at the end of the text, only if truncated. Examples (text == 'Lorem ipsum dolor sit amet', len(text) == 26) {{ text|truncatewords_by_chars:30 }} 'Lorem ipsum dolor sit amet' {{ text|truncatewords_by_chars:25 }} 'Lorem ipsum dolor sit...' {{ text|truncatewords_by_chars:21 }} 'Lorem ipsum dolor sit...' {{ text|truncatewords_by_chars:20 }} 'Lorem ipsum dolor...' By Davide Muzzarelli

  • filter
  • template-filter
  • truncate
  • truncatewords
Read More

Template filter that divides a list into exact columns

Template filter that divides a list into an exact number of columns. The number of columns is guaranteed. Example (list == [1,2,3,4,5,6,7,8,9,10]): {% for column in list|columns:3 %} <ul> {% for item in column %} <li>{{ item }}</li> {% endfor %} </ul> {% endfor %} Result: <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> </ul> <ul> <li>5</li> <li>6</li> <li>7</li> </ul> <ul> <li>8</li> <li>9</li> <li>10</li> </ul> By Davide Muzzarelli

  • filter
  • template-filter
  • list
  • columns
Read More

davmuz has posted 4 snippets.