Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 26, 2010 08:05:37

Igor A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Access to request from tag


Hi

I need to make an custom tag, for auth checks, based on data stored on
cookies. Like a:
{% if_loggen_in %}
<span>Some message for logged in only user</span>
{% end_if_logged_in %}

btw, i don't know how to get access to request object in my tag. I know that
there is 'inclusion tags', but as you see there is nothing to include, my
tag just renders data based on current request data.

I know about .request middleware and RequestContext class, but this tag can
be used anywhere (and at least it used in main layout, so it used in every
page), so the way when I use RequestContext(context) in every possible view
is too complex.

How can i get access to it from custom tag? Can i make it use RequestContext
by default?

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

Offline

#2 Dec. 26, 2010 09:55:30

P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Access to request from tag


Hi.

You want to look at template context processors.http://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontextYou can create your own context processors or use the built in. These
can provide access to the current request in your template… and
therefore request.user.is_authenticated (or something like that) :)

hope it helps

pete


On 26 Dec., 09:05, Igor Artamonov <igor.artamo...@gmail.com> wrote:
> Hi
>
> I need to make an custom tag, for auth checks, based on data stored on
> cookies. Like a:
> {% if_loggen_in %}
> <span>Some message for logged in only user</span>
> {% end_if_logged_in %}
>
> btw, i don't know how to get access to request object in my tag. I know that
> there is 'inclusion tags', but as you see there is nothing to include, my
> tag just renders data based on current request data.
>
> I know about .request middleware and RequestContext class, but this tag can
> be used anywhere (and at least it used in main layout, so it used in every
> page), so the way when I use RequestContext(context) in every possible view
> is too complex.
>
> How can i get access to it from custom tag? Can i make it use RequestContext
> by default?

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

Offline

#3 Dec. 26, 2010 09:57:55

P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Access to request from tag


Hi.

You can use template context processors.http://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-context-requestcontextyou can either use the built in processors or write your own. They
just have to be added in the settings file(all this is explained in
the docs)

These can provide you access to, ex. the current HTTPrequest in your
template… and therefore you would be able to write something like
{% if request.user.is_authenticated %}
welcome
{% else %}
login
{% endif %}

hope it helps :)

pete

On 26 Dec., 09:05, Igor Artamonov <igor.artamo...@gmail.com> wrote:
> Hi
>
> I need to make an custom tag, for auth checks, based on data stored on
> cookies. Like a:
> {% if_loggen_in %}
> <span>Some message for logged in only user</span>
> {% end_if_logged_in %}
>
> btw, i don't know how to get access to request object in my tag. I know that
> there is 'inclusion tags', but as you see there is nothing to include, my
> tag just renders data based on current request data.
>
> I know about .request middleware and RequestContext class, but this tag can
> be used anywhere (and at least it used in main layout, so it used in every
> page), so the way when I use RequestContext(context) in every possible view
> is too complex.
>
> How can i get access to it from custom tag? Can i make it use RequestContext
> by default?

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

Offline

#4 Dec. 26, 2010 09:59:00

P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Access to request from tag


didn't mean to spam your thread… thought my answer was lost… :-S

On 26 Dec., 10:57, PeteDK <petermoel...@gmail.com> wrote:
> Hi.
>
> You can use template context processors.
>
>http://docs.djangoproject.com/en/dev/ref/templates/api/#subclassing-c...
>
> you can either use the built in processors or write your own. They
> just have to be added in the settings file(all this is explained in
> the docs)
>
> These can provide you access to, ex. the current HTTPrequest in your
> template… and therefore you would be able to write something like
> {% if request.user.is_authenticated %}
> welcome
> {% else %}
> login
> {% endif %}
>
> hope it helps :)
>
> pete
>
> On 26 Dec., 09:05, Igor Artamonov <igor.artamo...@gmail.com> wrote:
>
>
>
> > Hi
>
> > I need to make an custom tag, for auth checks, based on data stored on
> > cookies. Like a:
> > {% if_loggen_in %}
> > <span>Some message for logged in only user</span>
> > {% end_if_logged_in %}
>
> > btw, i don't know how to get access to request object in my tag. I know that
> > there is 'inclusion tags', but as you see there is nothing to include, my
> > tag just renders data based on current request data.
>
> > I know about .request middleware and RequestContext class, but this tag can
> > be used anywhere (and at least it used in main layout, so it used in every
> > page), so the way when I use RequestContext(context) in every possible view
> > is too complex.
>
> > How can i get access to it from custom tag? Can i make it use RequestContext
> > by default?

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

Offline

#5 Dec. 26, 2010 15:10:55

Igor A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Access to request from tag


Yes, thank you Pete, but i know about this way. And it requires to use
RequestContext class on _every_ view.
I have too much of them, and also it makes boilerplate code. So, i'm
looking for less complicated way.
Maybe there is exist some way to use RequestContext by default?

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

Offline

#6 Dec. 26, 2010 15:28:15

P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Access to request from tag


Ahh my bad. didn't see the last part of your post where you said you
had already tried the processor way :)

however the docs. mention thread safety concerns when it comes to
custom tags.http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#thread-safety-considerationsSo perhabs it's not so safe to link RequestContext to a custom
template tag??

besides that I'm afraid I can't be of any assistance :) good luck.

pete

On 26 Dec., 16:10, Igor Artamonov <igor.artamo...@gmail.com> wrote:
> Yes, thank you Pete, but i know about this way. And it requires to use
> RequestContext class on _every_ view.
> I have too much of them,  and also it makes boilerplate code. So, i'm
> looking for less complicated way.
> Maybe there is exist some way to use RequestContext by default?

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

Offline

#7 Dec. 26, 2010 16:52:53

Igor A.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Access to request from tag


BTW, thank you very much.

I googled a lot, and decided that easiest way is replace all my "Context("
with "RequestContext(request," and use official way.
That works :)

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

Offline

#8 Dec. 26, 2010 17:16:34

Mikhail K.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Access to request from tag


Hi Igor,

If you use django <= 1.2 the you can replace 'render_to_response' with
'direct_to_template'. They do almost the same but 'direct_to_template'
uses RequestContext by default:

from django.views.generic.simple import direct_to_template

def my_view(request):
# ...
return direct_to_template(request, 'my_template.html', {'foo':
'bar'})

In django trunk there are 2 less hacky shortcuts that can help:http://docs.djangoproject.com/en/dev/topics/http/shortcuts/#renderhttp://docs.djangoproject.com/en/dev/ref/template-response/#using-templateresponse-and-simpletemplateresponseOn 26 дек, 21:52, Igor Artamonov <igor.artamo...@gmail.com> wrote:
> BTW, thank you very much.
>
> I googled a lot, and decided that easiest way is replace all my "Context("
> with "RequestContext(request," and use official way.
> That works :)

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

Offline

Board footer

Moderator control

Enjoy the 12th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support