Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
when is a Context Processor called?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Stefano Tranquillini  
View profile  
 More options Oct 7 2012, 12:43 pm
From: Stefano Tranquillini <stefano.tranquill...@gmail.com>
Date: Sun, 7 Oct 2012 18:42:20 +0200
Local: Sun, Oct 7 2012 12:42 pm
Subject: when is a Context Processor called?

Hi all.
i just discovered the context processor, and i use it for put an object in
the request automatically, this is the code:

def addProfile(request):
    try:
        userProfile = UserProfile.objects.get(user=request.user)
        return {'user_profile':userProfile}
    except:
        return {}

this is the setting.py

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.contrib.messages.context_processors.messages',
    'social_auth.context_processors.social_auth_by_type_backends',
    'earth.context_processors.addProfile',
)

now, it works, except one case.
i've in a html page ajax call that sends some data to an url, here the JS:

$.post("/geoloc/updateloc/", { latitude: lat, longitude: lon });

mapped as

    url(r'^geoloc/updateloc/$', 'earth.views.updateLoc'),

and here is the view:

@login_required
@csrf_protect
def updateLoc(request):
    message={}
    message['status']='ko'
    if request.is_ajax():
        if request.method == 'POST':
            message['status']='ok'
            userProfile = request.user_profile
            userProfile.latitude=request.POST['latitude']
            userProfile.longitude=request.POST['longitude']
            userProfile.save()
            # Here we can access the POST data
    return HttpResponse(json.dumps(message), mimetype="application/json")

the fact is that in the view, the request.user_profile (which should be
loaded by the context template) is empty or none. basically if i print it i
don't have anything printed.

basically: when is my context processor called?
is it called  only for render_to_response or also for redirect or
HTTPResponse or HTTPResponseRedirect?
what should i do?

thanks

ciao

--
Stefano


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Roseman  
View profile  
 More options Oct 7 2012, 2:37 pm
From: Daniel Roseman <dan...@roseman.org.uk>
Date: Sun, 7 Oct 2012 11:37:01 -0700 (PDT)
Local: Sun, Oct 7 2012 2:37 pm
Subject: Re: when is a Context Processor called?

The name of the setting should give you a clue: TEMPLATE_CONTEXT_PROCESSOR.
Context processors are for doing stuff to template contexts. They have
nothing whatsoever to do with views. If you're not using a template, then
context processor won't help you.
--
DR.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stefano T  
View profile  
 More options Oct 8 2012, 10:28 am
From: Stefano T <stefano.tranquill...@gmail.com>
Date: Mon, 8 Oct 2012 07:28:43 -0700 (PDT)
Local: Mon, Oct 8 2012 10:28 am
Subject: Re: when is a Context Processor called?

Ok.
so basically they are called before the rendering of a template.

How can i have an object stored in the request object (if possible)?

Something that lets me to do, in a view: request.user_profile...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kurtis Mullins  
View profile  
 More options Oct 8 2012, 10:55 am
From: Kurtis Mullins <kurtis.mull...@gmail.com>
Date: Mon, 8 Oct 2012 10:53:53 -0400
Local: Mon, Oct 8 2012 10:53 am
Subject: Re: when is a Context Processor called?

https://docs.djangoproject.com/en/dev/topics/http/sessions/

On Mon, Oct 8, 2012 at 10:28 AM, Stefano T
<stefano.tranquill...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tom Evans  
View profile  
 More options Oct 9 2012, 7:03 am
From: Tom Evans <tevans...@googlemail.com>
Date: Tue, 9 Oct 2012 12:02:57 +0100
Local: Tues, Oct 9 2012 7:02 am
Subject: Re: when is a Context Processor called?
On Mon, Oct 8, 2012 at 3:28 PM, Stefano T

<stefano.tranquill...@gmail.com> wrote:
> Ok.
> so basically they are called before the rendering of a template.

> How can i have an object stored in the request object (if possible)?

> Something that lets me to do, in a view: request.user_profile...

You can define a simple middleware to put attributes on a request. Eg:

class MyMiddleware(object):
  def process_request(request):
    request.profile = None
    if request.user.is_authenticated():
      request.profile = request.user.get_profile()

See the documentation on middleware:

https://docs.djangoproject.com/en/1.4/topics/http/middleware/

Note that anything you put in TEMPLATE_CONTEXT_PROCESSORS is run
whenever you render a template, and anything in MIDDLEWARE_CLASSES
will be run on each request, so make sure the things you do in those
places are actually required every time you render a template /
process a request.

Cheers

Tom


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stefano Tranquillini  
View profile  
 More options Oct 9 2012, 8:21 am
From: Stefano Tranquillini <stefano.tranquill...@gmail.com>
Date: Tue, 9 Oct 2012 14:19:48 +0200
Local: Tues, Oct 9 2012 8:19 am
Subject: Re: when is a Context Processor called?

ok,
but in this way when the user logs out i've to remove the object from the
request, right?
what if the user closes the browser?

--
Stefano

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marek Brzóska  
View profile  
 More options Oct 9 2012, 8:29 am
From: Marek Brzóska <brzoskama...@gmail.com>
Date: Tue, 9 Oct 2012 14:28:16 +0200
Local: Tues, Oct 9 2012 8:28 am
Subject: Re: when is a Context Processor called?

2012/10/9 Stefano Tranquillini <stefano.tranquill...@gmail.com>

> ok,
> but in this way when the user logs out i've to remove the object from the
> request, right?
> what if the user closes the browser?

No. The request object lives just for one request from browser. User clicks
tries to get certain url in browser, then django creates request object,
which you use in view - as first argument. With this middleware you will be
able to use request.profile if user has profile.
--

Marek Brzóska

brzoskama...@gmail.com


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
lacrymology@gmail.com  
View profile  
 More options Oct 9 2012, 9:31 am
From: "lacrymol...@gmail.com" <lacrymol...@gmail.com>
Date: Tue, 09 Oct 2012 13:30:24 +0000
Local: Tues, Oct 9 2012 9:30 am
Subject: RE: when is a Context Processor called?

For this example in particular, if you have django's UserMiddleware active, and that code assumes you do, you can always do request.user.get_profile() without needing to add this custom middleware

-----Mensaje original-----
De: Marek Brzóska
Enviados:  09/10/2012 09:28:16
Asunto:  Re: when is a Context Processor called?

2012/10/9 Stefano Tranquillini <stefano.tranquill...@gmail.com>

> ok,
> but in this way when the user logs out i've to remove the object from the
> request, right?
> what if the user closes the browser?

No. The request object lives just for one request from browser. User clicks
tries to get certain url in browser, then django creates request object,
which you use in view - as first argument. With this middleware you will be
able to use request.profile if user has profile.
--

Marek Brzóska

brzoskama...@gmail.com

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »