Login

Tag "dry"

Snippet List

DRY menu Custom Template Tag

This is the description of a custom template tag to create DRY menu. It solves the problem of markup duplication in templates of your site. The menu always has one active option and one or several inactive options. HOW TO USE Define a structure of your menu in a parent template: {% defmenu "menu1" %} {% active %}<span class='active'>__text__</span>{% endactive %} {% inactive %}<a href='__url__'>__text__</a>{% endinactive %} {% opt "opt1" "/opt1/" %}Go to opt1{% endopt %} {% opt "opt2" "/opt2/" %}Go to opt2{% endopt %} {% opt "opt3" "/opt3/" %}Go to opt3{% endopt %} {% enddefmenu %} The menu has it's name (first parameter of the tag 'defmenu'. First parameter of a tag 'opt' is menu option's name. '__text__' inside of 'active'/'inactive' will be substituted by inner text of a tag 'opt' (Go to opt...), '__url__' indide of 'active'/'inactive' will be substituted by second parameter of a tag 'opt' To generate menu with one selected option in child template do: {% menu "menu1" "opt1" %} Here: "menu1" is a name of menu that was defined by 'defmenu' tag, "opt1" is selected option. Result of the applying 'menu' is the next: <span class='active'> Go to opt1</span> <a href='"/opt2/"'>Go to opt2</a> <a href='"/opt3/"'>Go to opt3</a>

  • menu
  • dry
  • menubar
Read More

Simple DRY Tabs using Django 1.3

I was just playing around with Django 1.3 and came across the new 'with' addition to the include tag. I thought this was a super elegant way to implement a DRY tab menu without having to move menu code into the views. Enjoy!

  • menu
  • dry
  • tabs
  • menus
  • tabbed
  • tab
Read More

DRY template rendering decorator update

Based fully on [snippet 1929](http://www.djangosnippets.org/snippets/1929/) **The update is:** It checks to see the view returns an instance of HttpResponse and returns that rather than trying to render it. This allows you to return something like `HttpResponseRedirect('/')`, or use a normal `render_to_response` to use a different template. *Also updates cleandict as per comment on original snippet* In this case the 'render_template' decorator assumes there is a myview.html template. this keeps things simple and you DRY. Hope it helps.

  • template
  • decorator
  • rendering
  • dry
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

Showell markup--DRY up your templates

The code shown implements a preprocessor for Django templates to support indentation-based syntax. The pre-markup language is called Showell Markup. It allows you to remove lots of close tags and random punctuation. It also has a syntax for cleaning up individual lines of HTML with a pipe syntax for clearly separating content from markup. You can read the docstrings to glean the interface. Here are examples: >> table >> tr >> td Right >> td Center >> td Left >> div class="spinnable" >> ul >> li id="item1" One >> li id="item2" Two %% extends 'base.html' %% load smartif %% block body %% for book in books {{ book }} %% if condition Display this %% elif condition Display that %% else %% include 'other.html' >> tr class="header_row" Original Author | th class="first_column" Commenters | th Title | th Action | th Last words | th By | th When | th >> ol class="boring_list" One | li Two | li Three | li {{ element4|join:',' }} | li Hello World! | b | span class="greeting" ; br Goodbye World! | i | span class="parting_words"; br ; hr >> p class="praise" Indentation-based syntax is so Pythonic! Archive LINK collection.archive referral.pk Home LINK home.home Friends LINK friendship.friends Create a card LINK referrals.create Recent changes LINK activity.recent_changes >> FORM referrals.create {{ form.as_p }} {{ referral.id } | HIDDEN referral

  • templates
  • dry
  • preprocessor
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

DRY Fieldsets

I've devised a DRY method of declaring django fieldsets: ** Example usage: ** 1. Include the attached code in `fieldsets.py` 2. `models.py`: from django.db import models from fieldsets import Fieldset, ModelWithFieldsets class Person(ModelWithFieldsets): #instead of models.Model # this field will be placed in nameless fieldset example_field = models.IntegerField() # this fieldset will be grouped into one row Fieldset(grouped=True) first_name = models.CharField(max_length=64) surname = models.CharField(max_length=64) Fieldset("Contact Details", classes=('collapse',)) mobile_phone = models.CharField(max_length=10) email_address = models.EmailField() Fieldset("Address") street_address = models.CharField(max_length=255) # the next two fields will be grouped into one row of this fieldset Fieldset.new_group(2) suburb = models.CharField(max_length=64) state = models.CharField(max_length=64) 3. `admin.py`: from django.contrib import admin from models import Person from fieldsets import Fieldset class PersonAdmin(admin.ModelAdmin): fieldsets = Fieldset.get_fieldsets(Person) admin.site.register(Person, PersonAdmin) This example produces the equivalent of manually typing: fieldsets = ( (None, {'fields': ('example_field')}), (None, {'fields': (('first_name', 'surname'),)}), ('Contact Details', { 'fields': ('mobile_phone', 'email_address'), 'classes': ('collapse',)}), ('Address', {'fields': ('street_address', ('suburb', 'state'))}) ) But now if you want to rearrange your fields, rename, delete, insert, etc, you won't need to remember to update the fieldsets in the ModelAdmin. This implementation is a bit of a hack, but I believe a cleaner equivalent should be implemented in django itself.

  • admin
  • dry
  • fieldsets
Read More

DRY with common model fields (another way)

Some time you want to add some common fields to a group of models, for example, in a **Generalization/Specialization** relationship. One could have a base model as the generalization class and specialized models with a foreign key to that base model with an unique attribute but I don't like it that way so, I just do this code to add some commons attributes to a lot of models. If you have many models that all share the same fields, this might be an option. The fields are added directly to each model, e.g. while they will be duplicated on the database level, you only have to define them once in your **python** code. This code is a cleaner way(I think!!!) to do it and will do the same that [this one](http://www.djangosnippets.org/snippets/317/). I hope this piece of code will be useful for you.

  • models
  • model
  • dry
  • common
Read More

9 snippets posted so far.