On Fri, Aug 24, 2012 at 9:09 AM, Gregory Strydom
<gregory.strydom...@gmail.com> wrote:
> I'm at the end of  my rope lol.
>
> For the life of me i cant discern where you put Context definitions.
> Ive had a look at django tutorial and the documentation and unless im
> seriously overlooking something i cant find the information i need.
> I understand perfectly how it works.

If you understood perfectly, you wouldn't be asking these questions :)

>
> E.g:  P = Context()
> P['Name'] = "Fred"
> P["Age"] = "27"
>
> <html>
> <head>
> </head>
> <body>
> Hi my name is {{ P.Name }}
> </body>
> </html>
>
> I can understand that perfectly. What i cant understand is where do place
> the P = Context() P['Name'] = "Fred".
> Does it go in the models file, Views file, html file, seperate file??
>
> Im basically tearing my hair out in frustration as im sure its really simple
> but i just cant see it.
>
> If someone could someone please give an example or explain where i need to
> place it i would be extremely gratefull!!
>

Contexts are used when rendering templates. All function calls that
render templates also take two additional arguments, which can be used
to populate a context. Eg:

render_to_string(template_name, dictionary=None, context_instance=None)

The context_instance here is the Context you wish to use to render the
template. The key-value-pairs in the dictionary are merged into a
supplied context_instance, or used to create a new Context if none is
supplied.

Finally, there is a sub-class of Context called RequestContext. This
is a context that is created with a request object. It will step
through each context processor defined in
settings.TEMPLATE_CONTEXT_PROCESSORS, and call the functioned
specified there, passing the request as the only argument. The
dictionary returned from this function is then merged into the
context.

All of this is documented on a single page here:

https://docs.djangoproject.com/en/1.4/ref/templates/api/

So, what does this mean? Well, typically on a "full fat" web page,
you'll want to use a RequestContext, so that common variables - like
the currently logged in user - are automatically available in your
template. Therefore, in a view, you will commonly have a pattern like
this:

def myview(request):
  ctxt = RequestContext(request)
  return render_to_response("myapp/myview.html", {
    'hello': 'world',
    'testing': '123', }, context_instance=ctxt)

Inside the template itself, the context is now the global scope.
Therefore, you don't refer to the 'name' of the context, eg in your
example, "{{ P.Name }}" is incorrect; the context has a variable
called 'Name' in it, so "{{ Name }}" is the correct usage.

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