1

I want to query all products and load them in a dict with category-id as key using a context processor. Then I want to make this dict available in a template, in order to send it to a template tag as argument.

Is it possible to use the context without a view? Your help is appreciated.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
blaise
  • 383
  • 1
  • 5
  • 16

2 Answers2

0

Context processors have nothing to do with views. They are run every time you create a RequestContext, which you often do as part of render_to_response but can certainly do outside of that.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • this is what I meant when I said view: the render_to_response thing. Can you please give me an example on how to create a RequestContext without render_to_response in order to make my dict available in a template? – blaise May 31 '12 at 12:56
0

Why "make the dict available in a template to send it to a template tag" when you can just load your products from the templatetag's code itself ?

wrt/ RequestContext, it's a very ordinary python class and there's no special magic involved: you just have to import the class and instanciate it.

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • I want to avoid repeating db query every time I want the products. In my case, let's say I have 3 categories per page, for each category, I want to automatically list related products (without a use click a category). So for each category, I call the template tag and send it the already available dict of products along with the category-id. The template tag would then filter the dict and send me back the relevant products. – blaise May 31 '12 at 15:14