Context Help

20 views
Skip to first unread message

Gregory Strydom

unread,
Aug 24, 2012, 4:09:29 AM8/24/12
to django...@googlegroups.com
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.

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

Tom Evans

unread,
Aug 24, 2012, 6:19:53 AM8/24/12
to django...@googlegroups.com
On Fri, Aug 24, 2012 at 9:09 AM, Gregory Strydom
<gregory.s...@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

Melvyn Sopacua

unread,
Aug 24, 2012, 6:31:26 AM8/24/12
to django...@googlegroups.com
On 24-8-2012 10:09, Gregory Strydom wrote:

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

With Class Based Views this is a lot simpeler:
<https://docs.djangoproject.com/en/1.4/topics/class-based-views/#adding-extra-context>
--
Melvyn Sopacua

Gregory Strydom

unread,
Aug 24, 2012, 7:26:51 AM8/24/12
to django...@googlegroups.com

Thanks for taking the time to help out i really appreciate it :)

Im still a little confused so i think the best thing to do would be to start from scratch, going through the tutorial again, and again and taking things really slowly (slower thanks snails pace lol), and work through it step by step until i get a better understanding.

I think im getting frustrated as well because i kind of rushed through the tutorial as i am currently on a deadline with my company to get this project completed. I will just need to explain it is going to take a little longer than i expected and as i said take things very slowly and make sure i understand each step before moving on to the next.

What you guys said does make a lot more sense though, thanks again!
Reply all
Reply to author
Forward
0 new messages