Support for macros in templates
Hi all, This is a little announcement of a tag library for {% macro %} support in Django templates. I have been using TAL template engine in my previous project and got used to the concept of "macros" as building blocks of the final HTML output. Now in Django I realised the missing support for "macros" is a serious limitation. I admit I may be too locked to a different mindset and perhaps the same things I need macros for could be achieved with pure Django templates. Maybe, maybe not. Anyway, I was missing macros so much that I decided to add them to Django. If interested, take the library from here: http://www.djangosnippets.org/snippets/363/ Usage is like this: 1) Load the macro library: {% load macros %} 2) Define a new macro called 'my_macro' with parameter 'arg1': {% macro my_macro arg1 %} Parameter: {{ arg1 }} {% endmacro %} 3) Use the macro with a String parameter: {% usemacro my_macro "String parameter" %} or with a variable parameter (provided the Context defines 'somearg' variable, e.g. with value "Variable parameter"): {% usemacro my_macro somearg %} The output of the above code would be: Parameter: String parameter Parameter: Variable parameter It works pretty well and the only drawback is that the defined macros are local to each template file, i.e. are not inherited through {%extends ...%} and you can't have for instance some common macros defined in a separate file and include that file wherever needed. I'll try to solve that problem later. Perhaps through a new tag, for instance {%macrofile ...%} or something like that. Maybe someone will contribute a patch? ;-) Enjoy and sorry for the spam Michal -- * http://www.logix.cz/michal --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Support for macros in templates
olivier wrote: > Hi Michal, > > On 11 août, 04:51, Michal Ludvig <[EMAIL PROTECTED]> wrote: > >> It works pretty well and the only drawback is that the defined macros >> are local to each template file, i.e. are not inherited through >> {%extends ...%} and you can't have for instance some common macros >> defined in a separate file and include that file wherever needed. I'll >> try to solve that problem later. Perhaps through a new tag, for instance >> {%macrofile ...%} or something like that. Maybe someone will contribute >> a patch? ;-) > > > Are you aware of Jinja (http://jinja.pocoo.org/) ? Yes I am and actually checked that out before writing the macro support myself. The difference is that Jinja is a completely independent template engine, while my code is just a loadable extension to standard Django templates. And given that Jinja templates aren't 100% compatible with Django it may not be easy to switch from django.template to jinja just to get macros. My {% macro %} library does what I needed, nothing more. And it has been a learning opportunity as well, as I haven't developed any django template tags before ;-) Michal -- * http://www.logix.cz/michal --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Database Fields
b3n wrote: > Why doesn't Django create primary key integer and tinyint integer > database fields as UNSIGNED? Assuming you're talking about MySQL ... > Especially where a field is used as a flag. e.g. auth_user > > is_staff = tinyint(1) > is_active = tinyint(1) > is_superuser = tinyint(1) > > Doesn't seem very optimised. UNSIGNED is just a flag, the field size is always 1 byte regardless if it's signed or unsigned. > And why does it create primary key integer fields with length (11) ? That "11" is a maximum display length in decimal representation of 4byte long integer. Don't worry, it only takes 4 bytes. Michal --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Database Fields
b3n wrote: > Thanks, > > OK, but if a field value is only ever going to be 0 or 1, why make it > signed? It's misleading. Signed is MySQL default. Use e.g. PositiveSmallIntegerField if you want it unsigned. > And what is the purpose of INT(11), why should it be that instead of > INT(10) ? Again, MySQL default for INT fields. The "11" is misleading, I agree, but blame MySQL, not Django for that. Django only creates an INT field and MySQL shows it as INT(11), that's all. For you it's only a visual inconvenience, nothing performance related or things like that. > After looking at the created tables, I'm going off Django! =/ A bit unfair, Django just created whatever was specified in *your* model. Suggested reading: http://www.djangoproject.com/documentation/model-api Michal --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
[solution] Re: Template Macros Like In Jinja
Hi Papa > i wonder if there is any way to have Macros in django templates > similar to what Jinja has (http://jinja.pocoo.org/)? Have a look at "Support for {% macro %} tags in templates" at http://www.djangosnippets.org/snippets/363/ I bet it's exactly what you're after. It lets you define macros with {%macro%} tag and use it with {%usemacro%}, you can load macros from external file with {%loadmacros%}, etc. Hope that helps Michal --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Custom template tag and filters in resolve_variable
Hi all, for the project I'm working on I needed to create a custom template tag, call it 'mytag'. If I use it as {% mytag something %} everything goes fine (where 'something' is a string passed to the template). However I can't add any filters to that 'something'. As soon as I do: {% mytag something|upper %} I get an exception: VariableDoesNotExist at /test/ Failed lookup for key [something|upper] That leads to a code in MytagNode.render() method: actual_message = resolve_variable(self.message, context) where self.message is the argument passed to the macro, in this case it's a string 'something|upper'. Is there an easy way to run all the attached filters in MytagNode? Thanks! Michal --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Custom template tag and filters in resolve_variable
Rune Bromer wrote: > On Dec 5, 2007, at 11:59 AM, Michal Ludvig wrote: > >> Hi all, >> >> for the project I'm working on I needed to create a custom template >> tag, >> call it 'mytag'. >> >> If I use it as {% mytag something %} everything goes fine (where >> 'something' is a string passed to the template). >> However I can't add any filters to that 'something'. As soon as I do: >> {% mytag something|upper %} >> I get an exception: >> VariableDoesNotExist at /test/ >> Failed lookup for key [something|upper] >> >> That leads to a code in MytagNode.render() method: >> actual_message = resolve_variable(self.message, context) >> where self.message is the argument passed to the macro, in this case >> it's a string 'something|upper'. >> >> Is there an easy way to run all the attached filters in MytagNode? > > Look at the "filter" templatetag [1]. You must do something like > > {% filter upper %} {% mytag something %} {% endfilter %} That seems to run the whole output of mytag through the filter. I just need the parameter itself filtered. In reality mytag gets two arguments, 'label' and 'value' and spits out a few lines of HTML. It displays some user profile information. The value of each property may need to be filtered differently, e.g. if it's a dollar amount I want to attach my filter '|dollarformat' that turns an integer like 1000 to a more readable "$1,000". For other properties I may want other filters. In any case I only need to filter the argument, not the tag output. I could probably do something like message.split("|") and run the filters (if any) manually. That is, import them from django.template.defaultfilters and pass the output of resolve_variable through them. But what if the filter in question is imported from a non-default tag library? How do I find what to import then? BTW Wouldn't it be more user-friendly to resolve the variable and run all the filters yet before the custom tag gets called? Michal --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
(solved) Re: Custom template tag and filters in resolve_variable
Michal Ludvig wrote: > for the project I'm working on I needed to create a custom template tag, > call it 'mytag'. > > If I use it as {% mytag something %} everything goes fine (where > 'something' is a string passed to the template). > However I can't add any filters to that 'something'. As soon as I do: > {% mytag something|upper %} > I get an exception: > VariableDoesNotExist at /test/ > Failed lookup for key [something|upper] > > That leads to a code in MytagNode.render() method: > actual_message = resolve_variable(self.message, context) > where self.message is the argument passed to the macro, in this case > it's a string 'something|upper'. > > Is there an easy way to run all the attached filters in MytagNode? For the record: There is a FilterExpression class in django.template that does exactly what I need. So for the tag implementation, instead of: === from django.template import resolve_variable @register.tag def mytag(parser, token): tag_name, message = token.split_contents() [...] return MytagNode(message) class MytagNode(template.Node): [...] def render(self, context): actual_message = resolve_variable(self.message, context) [...] === I do it like this: === from django.template import FilterExpression @register.tag def mytag(parser, token): [...] filter_expression = FilterExpression(message, parser) return MytagNode(filter_expression) class MytagNode(template.Node): [...] def render(self, context): actual_message = self.filter_expression.resolve(context) [...] === The filter_expression.resolve(context) runs all the filters attached to the variable and things work as expected. The 'parser' argument even makes it aware of custom filters loaded through {% load ... %} tags. Lesson learned, note taken. Michal --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
How to get SQL "NOT IN" query from filter()
Hi all, I have a simple model with 'id' as a primary key and I have a list of IDs that were already processed in variable 'processed_ids'. How can I retrieve all objects that are *not* in that list? Something like "Model.objects.filter(id__not_in = processed_ids)". Unlike filter(id__in=...) which works just fine the __not_in modifier apparently isn't understood by django. I hope I won't have to fetch everything and then filter manually in a loop. Any ideas? Thanks! Michal --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: How to get SQL "NOT IN" query from filter()
Tim Chase wrote: >> How can I retrieve all objects that are *not* in that list? >> >> Something like "Model.objects.filter(id__not_in = processed_ids)". >> Unlike filter(id__in=...) which works just fine the __not_in modifier >> apparently isn't understood by django. > > > As commented recently on the list, you want "filter()"'s evil > twin "exclude()": > > exclude(id_in=...) That's it. Thanks. Now with the above example if I don't have a list of IDs but a list of the processed objects I'm not able to pass these as an argument to the __in operator. This: Model.objects.filter(...).exclude(id__in = processed) gives me: Truncated incorrect DOUBLE value: 'Model object' I have to use id__in=[p.id for p in processed] instead. Am I doing something wrong again or is it expected behaviour? Thanks Michal --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Filter ForeignKey values in model's admin view
Hi I've got two Django models: Contact and Group, where Group has two fields: contact and contact_primary linked to Contact. Like this: |class Group(models.Model): name = models.CharField(max_length=200) contacts = models.ManyToManyField(Contact) contact_primary = models.ForeignKey(Contact)| In the admin interface I can select a number of Contacts for the contacts field and then I want to see only these selected contacts in the contact_primary dropdown. Not necessarily immediately but definitely on subsequent admin view reloads. As it is now I can see all my Contacts, whether they're selected in the ManyToMany field or not. How can I restrict the Contacts displayed for contact_primary only to those selected in contacts? Thanks! Michal -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/528AE4A2.7080702%40logix.cz. For more options, visit https://groups.google.com/groups/opt_out.
Re: Filter ForeignKey values in model's admin view
On 19/11/13 17:10, Michal Ludvig wrote: > > Hi > > I've got two Django models: Contact and Group, where Group has two > fields: contact and contact_primary linked to Contact. Like this: > > |class Group(models.Model): > name = models.CharField(max_length=200) > contacts = models.ManyToManyField(Contact) > contact_primary = models.ForeignKey(Contact)| > > In the admin interface I can select a number of Contacts for the > contacts field and then I want to see only these selected contacts in > the contact_primary dropdown. Not necessarily immediately but > definitely on subsequent admin view reloads. As it is now I can see > all my Contacts, whether they're selected in the ManyToMany field or not. > > How can I restrict the Contacts displayed for contact_primary only to > those selected in contacts? > Anyone? I don't think it should be too difficult but can't put all the pieces together. In other words what I need is - in the Admin interface there's a drop-down list for "contact_primary" and a multi-select for "contacts". The "contacts" displays Contact.objects.all() - that's good, but so does "contact_primary" and that's not good. What I only want to display there is the particular group's group.contacts.all() - i.e. only those Contacts selected in the multi-select field. I'm sure it must be doable but I don't know how. Do I need a GroupAdmin(admins.ModelAdmin) class and somehow filter it in there? BTW I don't mind that upon creating a new Group the drop-down for contact_primary will be empty, I can live with that. Any ideas? Thanks! Michal -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/528BD7CD.9030304%40logix.cz. For more options, visit https://groups.google.com/groups/opt_out.