#4829: verbose_name(_plural) not accessible in templates (contrib.contenttype)
---------------------------------------------------------+------------------
   Reporter:  David Danier <[EMAIL PROTECTED]>  |                Owner:  nobody 
          
     Status:  new                                        |            
Component:  Contrib apps     
    Version:  SVN                                        |           
Resolution:                   
   Keywords:                                             |                
Stage:  Ready for checkin
  Has_patch:  1                                          |           
Needs_docs:  0                
Needs_tests:  0                                          |   
Needs_better_patch:  0                
---------------------------------------------------------+------------------
Comment (by David Danier <[EMAIL PROTECTED]>):

 Sorry to interrupt... :)

 After using my patch above a while I figured, that it has a serious
 disadvantage: You are only able to print the model-name _if_ you use
 `ContentType`.

 So I created a filter that does the same:
 {{{
 from django import template
 from django.utils.encoding import smart_str

 register = template.Library()

 def model_verbose_name(obj):
         return obj._meta.verbose_name
 register.filter(model_verbose_name)

 def model_verbose_name_plural(obj):
         return obj._meta.verbose_name_plural
 register.filter(model_verbose_name_plural)
 }}}

 Of course it would be nice to have access to the two attributes without
 using a filter, but I think this is enough for most people. And because it
 is in its own template-filter module here I added these two, too:
 {{{
 def order_by(queryset, arg):
         order_by = arg.split(',')
         return queryset.order_by(*order_by)
 register.filter(order_by)

 def filter(queryset, args):
         args = args.split(',')
         kwargs = {}
         for arg in args:
                 if '=' in arg:
                         k, v = arg.split('=', 1)
                         kwargs[smart_str(k)] = v
         return queryset.filter(**kwargs)
 register.filter(filter)
 }}}

 Because its nice to use this methods inside the template, mainly for some
 generic templates that should/could not get all possible parameters passed
 otherwise. With this you can use relations inside templates:
 {{{
         {{
 user.logentry_set|order_by:"-action_time"|filter:"action_flag=1"|slice:":5"
 }}
 }}}

 This now can be added to some generic template (e.g. a base-template that
 displays some log inside some sort of menu) without having to create the
 right list in every single view. And because the relations are there I
 don't see any reason not to use them this way.

 If mainstream is interested I will provide a patch for this. ;-)[[BR]]
 (Would put it into its one template-module so it is not loaded everywhere.
 Hope this is not possible using some existing code, didn't find anything.)

-- 
Ticket URL: <http://code.djangoproject.com/ticket/4829#comment:2>
Django Code <http://code.djangoproject.com/>
The web framework for perfectionists with deadlines
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to