On Sunday, 7 October 2012 17:43:19 UTC+1, Stefano T wrote:

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


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 received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/2lklE1ekuCYJ.
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.

Reply via email to