On Fri, Feb 4, 2011 at 3:30 PM, bruno desthuilliers
<bruno.desthuilli...@gmail.com> wrote:
> On 4 fév, 16:19, J J <dja...@dangeometer.com> wrote:
>> I have a inclusion tag that needs to access a named param in the url.  From
>> what I understand inclusion tags can only get information from
>> RequestContext.
>>
>> I seem to have solved my problem by putting the value into RequestContext
>> from the view that calls the page /main/site/01/2011.
>
> If you enable the "request" context processor in your settings AND use
> a ContextRequest in your views, you can access the request (and from
> there the GET params) from the context, so you don't need to manage
> this at the view level.
>
> http://docs.djangoproject.com/en/1.2/ref/templates/api/#django-core-context-processors-request
>

Whilst Bruno's advice is correct (apart from it is called
RequestContext, not ContextRequest), there is also another option. You
can add your own template context processor that extracts the variable
from the request and populates it into the context.

Custom template context processors are callables that take the request
as the only argument, and return a dictionary of values to be merged
into the context.

If this context processor should only be used on a few views, then you
probably wouldn't want to add it to
settings.TEMPLATE_CONTEXT_PROCESSORS, as it would run on every
request. Instead, you can supply a list of additional context
processors as the third argument to the RequestContext constructor.

So, an example:

def mycontextprocessor(request):
  return { 'my_foo': request.GET.get('myfoo'), }

def myview(request):
  ctxt = { 'foo': 'bar', }
  rctxt = RequestContext(request, ctxt, [ mycontextprocessor, ])
  return render_to_response('foo.html', context_instance=rctxt)

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

Reply via email to