Re: Changing template variables

2010-03-05 Thread Kevin Renskers
For those interested:

In my base template I've added this:

{% load custom_tags %}
{% if form %}
{% formerrors request form %}
{% endif %}

In my custom_tags template tags library:

@register.simple_tag
def formerrors(request, form):
for field, errors in form.errors.items():
for error in errors:
if field == '__all__':
messages.error(request, error)
else:
messages.error(request, field+': '+error)

return ''


Works perfectly for me: now all form errors are shown in exactly the
same way as all other messages in my application.

Cheers,
Kevin


On Mar 4, 4:33 pm, Kevin Renskers  wrote:
> I'll explain a bit more what precisely it is what I want to do: Django
> 1.2 comes with a new messages framework that allows for each message
> to have a different "level" (succes, error, warning, etc). I want to
> see if a form has errors, and if so, make a message for each error so
> all my notices and errors are displayed in the same way in my
> application.
>
> But I think I just came up with a solution: make a new template tag in
> my base template, always give it the form variable (whether it exists
> or not), and from there create the new messages.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: Changing template variables

2010-03-04 Thread Kevin Renskers
I'll explain a bit more what precisely it is what I want to do: Django
1.2 comes with a new messages framework that allows for each message
to have a different "level" (succes, error, warning, etc). I want to
see if a form has errors, and if so, make a message for each error so
all my notices and errors are displayed in the same way in my
application.

But I think I just came up with a solution: make a new template tag in
my base template, always give it the form variable (whether it exists
or not), and from there create the new messages.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: Changing template variables

2010-03-04 Thread Bill Freeman
Write your new view so that it can be used generically.

On Thu, Mar 4, 2010 at 10:22 AM, Kevin Renskers  wrote:
> Well yes, but I do not want to change all of my views. I want a
> generic solution to change template variables before they get
> rendered.
>
>
> On Mar 4, 4:14 pm, Bill Freeman  wrote:
>> Write your own view instead of using direct_to_template.
>>
>>
>>
>> On Thu, Mar 4, 2010 at 10:06 AM, Kevin Renskers  wrote:
>> > Hi,
>>
>> > I am wondering if it is possible to change template variables before
>> > they get rendered in a template.
>>
>> > For example, I use something like this in my template:
>> > return direct_to_template(request, template='index.html',
>> > extra_context={'form':form})
>>
>> > I would like to extend this form variable before the index.html
>> > template gets rendered.
>>
>> > My first though was making a template context processor, but I can't
>> > seem to be able to access template variables from it. My next thought
>> > was creating a piece of middleware with a process_response function,
>> > but also from this function it seems impossible to access the template
>> > variables.
>>
>> > Anyone got an idea on how to do this?
>>
>> > Thanks in advance,
>> > Kevin
>>
>> > --
>> > You received this message because you are subscribed to the Google Groups 
>> > "Django users" group.
>> > To post to this group, send email to django-us...@googlegroups.com.
>> > To unsubscribe from this group, send email to 
>> > django-users+unsubscr...@googlegroups.com.
>> > For more options, visit this group 
>> > athttp://groups.google.com/group/django-users?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@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.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: Changing template variables

2010-03-04 Thread Kevin Renskers
Well yes, but I do not want to change all of my views. I want a
generic solution to change template variables before they get
rendered.


On Mar 4, 4:14 pm, Bill Freeman  wrote:
> Write your own view instead of using direct_to_template.
>
>
>
> On Thu, Mar 4, 2010 at 10:06 AM, Kevin Renskers  wrote:
> > Hi,
>
> > I am wondering if it is possible to change template variables before
> > they get rendered in a template.
>
> > For example, I use something like this in my template:
> > return direct_to_template(request, template='index.html',
> > extra_context={'form':form})
>
> > I would like to extend this form variable before the index.html
> > template gets rendered.
>
> > My first though was making a template context processor, but I can't
> > seem to be able to access template variables from it. My next thought
> > was creating a piece of middleware with a process_response function,
> > but also from this function it seems impossible to access the template
> > variables.
>
> > Anyone got an idea on how to do this?
>
> > Thanks in advance,
> > Kevin
>
> > --
> > You received this message because you are subscribed to the Google Groups 
> > "Django users" group.
> > To post to this group, send email to django-us...@googlegroups.com.
> > To unsubscribe from this group, send email to 
> > django-users+unsubscr...@googlegroups.com.
> > For more options, visit this group 
> > athttp://groups.google.com/group/django-users?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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: Changing template variables

2010-03-04 Thread Bill Freeman
Write your own view instead of using direct_to_template.

On Thu, Mar 4, 2010 at 10:06 AM, Kevin Renskers  wrote:
> Hi,
>
> I am wondering if it is possible to change template variables before
> they get rendered in a template.
>
> For example, I use something like this in my template:
> return direct_to_template(request, template='index.html',
> extra_context={'form':form})
>
> I would like to extend this form variable before the index.html
> template gets rendered.
>
> My first though was making a template context processor, but I can't
> seem to be able to access template variables from it. My next thought
> was creating a piece of middleware with a process_response function,
> but also from this function it seems impossible to access the template
> variables.
>
> Anyone got an idea on how to do this?
>
> Thanks in advance,
> Kevin
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@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.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.



Changing template variables

2010-03-04 Thread Kevin Renskers
Hi,

I am wondering if it is possible to change template variables before
they get rendered in a template.

For example, I use something like this in my template:
return direct_to_template(request, template='index.html',
extra_context={'form':form})

I would like to extend this form variable before the index.html
template gets rendered.

My first though was making a template context processor, but I can't
seem to be able to access template variables from it. My next thought
was creating a piece of middleware with a process_response function,
but also from this function it seems impossible to access the template
variables.

Anyone got an idea on how to do this?

Thanks in advance,
Kevin

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@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.