@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?
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:
> @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.
>> @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.
>>> @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.
> 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.
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.
On Tue, Oct 9, 2012 at 1:02 PM, Tom Evans <tevans...@googlemail.com> wrote:
> 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()
> 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 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.
> 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.
--
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?
> 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.
--
-- 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.