Re: Template Question

2009-07-13 Thread CrabbyPete

Smartif is great, I don't get why it's not a standard part of django
templates.

On Jun 17, 5:02 pm, Steve Howell  wrote:
> Another option is to install the snippet below, which supports "in":
>
> http://www.djangosnippets.org/snippets/1350/
>
> On Jun 17, 1:53 pm, Ben Davis  wrote:
>
>
>
> > Nope, you'll need to set a variable in your view.  You can also try creating
> > your own filter such that {% if friend|is_in_group %}  would work (it's
> > pretty easy to do, just check out the docs for custom template filters)
>
> > On Tue, Jun 16, 2009 at 2:40 PM,CrabbyPete wrote:
>
> > > Is there a way do something like this with the template system
> > > {% if friend in group.members.all %} or simply {% friend in
> > > group.members.all %}- Hide quoted text -
>
> - Show quoted text -
--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Template Question

2009-06-17 Thread Steve Howell

Another option is to install the snippet below, which supports "in":

http://www.djangosnippets.org/snippets/1350/

On Jun 17, 1:53 pm, Ben Davis  wrote:
> Nope, you'll need to set a variable in your view.  You can also try creating
> your own filter such that {% if friend|is_in_group %}  would work (it's
> pretty easy to do, just check out the docs for custom template filters)
>
> On Tue, Jun 16, 2009 at 2:40 PM, CrabbyPete  wrote:
>
> > Is there a way do something like this with the template system
> > {% if friend in group.members.all %} or simply {% friend in
> > group.members.all %}
>
>

--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Template Question

2009-06-17 Thread Ben Davis
Nope, you'll need to set a variable in your view.  You can also try creating
your own filter such that {% if friend|is_in_group %}  would work (it's
pretty easy to do, just check out the docs for custom template filters)

On Tue, Jun 16, 2009 at 2:40 PM, CrabbyPete  wrote:

>
> Is there a way do something like this with the template system
> {% if friend in group.members.all %} or simply {% friend in
> group.members.all %}
> >
>

--~--~-~--~~~---~--~~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Template Question - What if...

2008-02-12 Thread Peter

Cool, thanks for the tip.

I took your latter suggestion and below is a rewrite of the
django.template.loader.get_template method.  The only additional
requirement is the FS_DIR constant that I import from my settings file
(which I was already using in settings.py to clean up my TEMPLATE_DIR
path declarations).

def get_template(template_name, widget_name):
from django.conf import settings
return _get_template(template_name, dirs=[settings.FS_DIR +
'widgets/' + str(widget_name)])

def _get_template(template_name, dirs=None):
 
'''
Returns a compiled Template object for the given template
name,
handling template inheritance
recursively.

The dirs argument is a list of directory
paths
'''
from django.template.loader import find_template_source,
get_template_from_string

source, origin = find_template_source(template_name, dirs)
template = get_template_from_string(source, origin, template_name)
return template


On Feb 12, 12:26 am, Malcolm Tredinnick <[EMAIL PROTECTED]>
wrote:
> On Mon, 2008-02-11 at 16:55 -0800, Peter wrote:
> > I have a bunch of widgets that I access dynamically and they render
> > their own parts of a page.  I would like to give these widgets the
> > ability to use templates.  Furthermore, these are not installed apps,
> > so I do not have access to the
> > "django.template.loaders.app_directories.load_template_source" loader
> > type.
>
> > So the widgets are like this:
>
> > project/
> >   |_widgets/
> >|_widget1/
> >|_widget2/
>
> > I initially decided to just make a templates directory in this
> > "widgets" folder where each widget could add its own folder of
> > templates -- sounds reasonable.
>
> > However, I'm curious about... WhatifI let these widgets put
> > templates directly in their own folders and I declare that project/
> > widgets is atemplatedirectory?  This seems a bit blasphemous, but
> > ideologies aside -- are there computational issues with this approach
> > (e.g. longer search for resolving templates, strange conflicts?)
>
> Just do it. There's no problem here. TEMPLATE_DIRECTORIES is just a list
> of directories that is searched by the filesystem loader for files that
> match the name you want to load. So the only drawback is that those
> directories are searched every time you load atemplate. Might be an
> issue, might not be.
>
> Alternatively,ifyou want to super-efficient and tricky, your widgets
> could load theirtemplatestrings manually: you can pass a "dirs"
> argument to the find_template_source() function, so you could manually
> alter the directories to search when loading the templates. Using a
> combination of find_template_source() and get_template_from_string(),
> just as get_template() does, means you can create aTemplateobject
> instance, loading the source from a restricted, custom set of
> directories. Have a poke around in django/templates/loader.py for
> details.
>
> Regards,
> Malcolm
>
> --
> The only substitute for good manners is fast 
> reflexes.http://www.pointy-stick.com/blog/
--~--~-~--~~~---~--~~
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: Template Question - What if...

2008-02-11 Thread Malcolm Tredinnick


On Mon, 2008-02-11 at 16:55 -0800, Peter wrote:
> I have a bunch of widgets that I access dynamically and they render
> their own parts of a page.  I would like to give these widgets the
> ability to use templates.  Furthermore, these are not installed apps,
> so I do not have access to the
> "django.template.loaders.app_directories.load_template_source" loader
> type.
> 
> So the widgets are like this:
> 
> project/
>   |_widgets/
>|_widget1/
>|_widget2/
> 
> I initially decided to just make a templates directory in this
> "widgets" folder where each widget could add its own folder of
> templates -- sounds reasonable.
> 
> However, I'm curious about... What if I let these widgets put
> templates directly in their own folders and I declare that project/
> widgets is a template directory?  This seems a bit blasphemous, but
> ideologies aside -- are there computational issues with this approach
> (e.g. longer search for resolving templates, strange conflicts?)

Just do it. There's no problem here. TEMPLATE_DIRECTORIES is just a list
of directories that is searched by the filesystem loader for files that
match the name you want to load. So the only drawback is that those
directories are searched every time you load a template. Might be an
issue, might not be.

Alternatively, if you want to super-efficient and tricky, your widgets
could load their template strings manually: you can pass a "dirs"
argument to the find_template_source() function, so you could manually
alter the directories to search when loading the templates. Using a
combination of find_template_source() and get_template_from_string(),
just as get_template() does, means you can create a Template object
instance, loading the source from a restricted, custom set of
directories. Have a poke around in django/templates/loader.py for
details.

Regards,
Malcolm

-- 
The only substitute for good manners is fast reflexes. 
http://www.pointy-stick.com/blog/


--~--~-~--~~~---~--~~
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: template question

2007-11-02 Thread RajeshD



On Nov 1, 9:56 am, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> I have this code in my view, for example:
>
> a=['a','b','c'] # a list of row labels
> b=[1,2,3] # a list of col label
> c=tab # an array (list of list) with len(a) rows and len(b) cols.

If c[i, j] previously had a value of x, change it to have a tuple of
the form:

c[i, j] = (x, a[i], b[j])

Thus, each c element now has its own value as well as its 'a' and 'b'
labels.

> then on the template side, it's easy to print c
> {% for row in c %}
>   
>   {% for col in row %}
> {{ col }}
>   {% endfor %}
>   
> {% endfor %}

Change: {{ col }} to

{{ col.0 }}, {{ col.1 }}, {{ col.2}}




--~--~-~--~~~---~--~~
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: template question

2007-11-02 Thread [EMAIL PROTECTED]

Nobody can help me ?

On 1 nov, 14:56, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> I have this code in my view, for example:
>
> a=['a','b','c'] # a list of row labels
> b=[1,2,3] # a list of col label
> c=tab # an array (list of list) with len(a) rows and len(b) cols.
> return render_to_response('template.html',{'a':a,'b':b,'c':c})
>
> then on the template side, it's easy to print c
> {% for row in c %}
>   
>   {% for col in row %}
> {{ col }}
>   {% endfor %}
>   
> {% endfor %}
>
> but in each cell, I want to add my col and rows label separate with
> comma with the c value ?
> how to index a and b below for having corresponding row and col
> labels ?
> {{ col }},{{ a }},{{ b }}


--~--~-~--~~~---~--~~
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: Template question

2007-06-03 Thread sansmojo

You could set an extra context variable in each view that tells
whether to display the div.  The variable is accessible from the
parent template.

base.html:
{% if show_inset %}
  
{% block inset %}{% endblock %}
  
{% endif %}

You could also use a template tag for the whole chunk, but this seems
simpler to me.

Branton


--~--~-~--~~~---~--~~
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: Template question

2007-06-02 Thread Eugene Morozov

On 2 июн, 17:37, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote:
> On Sat, 2007-06-02 at 06:07 -0700, Eugene Morozov wrote:
> > Hello,
> > I have a question which might be trivial to answer, but I didn't found
> > answer by scanning the docs.
> > In my base template I have defined inset block:
> >   
> > {% block inset %}{% endblock %}
> >   
> > I want to remove  tags if derived template
> > doesn't fill in the inset block. Of course I can add the tags to all
> > inset blocks in the derived templates, but this would look bad and
> > contradict DRY principle.
>
> No, there isn't any builtin way to do this. Is having an empty 
> element really a problem?

Thanks for answer. The problem is that the div has borders and it
messes design a little bit when it's empty.
Eugene


--~--~-~--~~~---~--~~
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: Template Question (for loop)

2006-07-28 Thread [EMAIL PROTECTED]

Well, you beat me to it, but I was going to say that a cutsom manager
is probably the way I'd do this. :)


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Template Question (for loop)

2006-07-28 Thread timster

That's not a bad solution.

I just came up with a solution I'm very happy with. I created a custom
manager that only returns visible objects.

This way I can just do categories = Category.objects.all() and
category.forum_set.all() in my template and it will only display
visible objects. It works great :)

class VisibleObjectsManager(models.Manager):
def get_query_set(self):
return super(VisibleObjectsManager,
self).get_query_set().filter(visible=True)

class Category(models.Model):
name = models.CharField(maxlength=50)
visible = models.BooleanField(default=True)
objects = VisibleObjectsManager()

class Forum(models.Model):
category = models.ForeignKey(Category)
name = models.CharField(maxlength=50)
visible = models.BooleanField(default=True)
objects = VisibleObjectsManager()


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Template Question (for loop)

2006-07-28 Thread [EMAIL PROTECTED]

Could create a list and loop through that, e.g.

cat_list = Category.objects.filter(visible=True)
list = []
for c in cat_list:
   list.append({"cat":c, "forums":c.forum_set.filter(visible=True,
category__visible=True})

Then:

{% for x in list %}
{{ c.cat.name }}
{% if c.forums %}
{% for forum in c.forums %}
...
{% endfor %}
{% else %}
...
{% endif %}
{% endfor %}

Chris


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Template Question (for loop)

2006-07-28 Thread timster

Yuck. I don't want to do it that way. It couples the display logic with
the template. If I ever need to change the criteria that determines
when to display forums/categories, I will need to change it in two
places (view and template).


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Template Question (for loop)

2006-07-28 Thread Javier Rivera

timster escribió:
> {% for category in categories %}
> {{ category.name }}
> {% if category.forum_set.count %}
> {% for forum in category.forum_set.all %}
> {{ forum.name }}
> {% endfor %}
> {% else %}
> no forums in this category
> {% endif %}
> {% endfor %}

Why not add some more ifs?

 > {% for forum in category.forum_set.all %}
+{% if forum.visible %}
 > {{ forum.name }}
+{% endif %}
 > {% endfor %}

Javier.

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Template question

2006-03-12 Thread Rob Slotboom

Hi Malcolm,

{{forloop.counter|add:offset}} works fine.

Thanks,
Rob


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Template question

2006-03-10 Thread limodou

On 3/10/06, Rob Slotboom <[EMAIL PROTECTED]> wrote:
>
> Is it possible to add some initial value to a forloop.counter?
>
> This would be handy when using limit and offset.
>
> More general, is is possible to use template vars as values for
> calculations:
> {{ var1 }} + {{ var 2 }}
>

I'v develop a custom tag for second thing, you can try it:

http://groups.google.com/group/django-users/browse_thread/thread/6c1c162e7dd5d0d8/ce3aa7e2e3d48ae1#ce3aa7e2e3d48ae1


--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit

--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: Template question

2006-03-10 Thread Malcolm Tredinnick

On Fri, 2006-03-10 at 02:41 -0800, Rob Slotboom wrote:
> Is it possible to add some initial value to a forloop.counter?
> 
> This would be handy when using limit and offset.
> 
> More general, is is possible to use template vars as values for
> calculations:
> {{ var1 }} + {{ var 2 }}

The first thing here is to always look at filters when you are trying to
do this sort of thing (there is indeed an "add" filter). The
documentation seems to suggest that using a variable as an argument to a
filter would not work (arguments must be in double quotes, etc), but the
source code resolves arguments correctly if they are variables as well.

So, in short, this appears to work (if "offset" is passed into the
context as the amount you want to add):

{{forloop.counter|add:offset}}

Tested on magic-removal branch, but I can't see anything that would have
changed to prevent it working in 0.91 either (test it, obviously).

Cheers,
Malcolm


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---