context dictionary

46 views
Skip to first unread message

Malik Rumi

unread,
Feb 23, 2016, 8:05:43 AM2/23/16
to Django users
from the docs:
Changed in Django 1.8:

The context argument used to be called dictionary. That name is deprecated in Django 1.8 and will be removed in Django 1.10.

https://docs.djangoproject.com/en/1.9/topics/http/shortcuts/#optional-arguments


I've been fooling with Django since 1.6 and I don't recall it ever being called "just" dictionary. But presumably it will still, in fact, be a dictionary?


I get the fact that the dictionary maps from Python objects to where on the template it goes, but that's really about all I've ever grasped about context. I've read the docs, but I don't feel like I really 'get it'.


Can someone here suggest another source - book, blog, online, off, that really goes into depth, and step by step explains context and all the various ways it can be created in a view? It would be especially helpful if this resource went into more advanced and complex contexts, like in a page pulling from many different models.


Thanks.

Andreas Kuhne

unread,
Feb 23, 2016, 8:25:10 AM2/23/16
to django...@googlegroups.com
Hi,

All you have to do is add what you want to the dictionary that you need for your template. There isn't anything "magic" about the context. So for example, if you want to have a model called "user" in the template, you can add it by writing: 

context = {}
context['user'] = User.objects.get(pk=user_id)

Fill the context with the information you want to present in the template for presentation. It's not much harder than that....

In the template you can then access the objects in the context object via {{user.fullName}} where you would get the fullname property from the user object.

Regards,

Andréas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/1e604039-1eda-4632-bc45-d0762e179eac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jorr...@gmail.com

unread,
Feb 23, 2016, 10:01:56 AM2/23/16
to Django users
Wouldn't including the name 'user' in your context dictionary override the authenticated user object in your template? Might make for some unexpected behavior.

Andreas Kuhne

unread,
Feb 23, 2016, 10:44:24 AM2/23/16
to django...@googlegroups.com
True, didn't know that it was active in that way nowadays. To Malik: Don't use user as a key in the dictionary.

Regards,

Andréas

2016-02-23 16:01 GMT+01:00 <jorr...@gmail.com>:
Wouldn't including the name 'user' in your context dictionary override the authenticated user object in your template? Might make for some unexpected behavior.
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Malik Rumi

unread,
Feb 24, 2016, 3:19:28 PM2/24/16
to django...@googlegroups.com
1. is { 5:five, 6:six} and {4:four) two dictionaries, or three?

2. Suppose I had a really long, or large, context, where the key is one of the 50 states and the value is the date of their admission to the Union (of the United States, of course). If that doesn't work, make it nations and the date they joined the UN. Is there a way to automate the creation of such a complex, or long, context? Yes, of course the data will be wildly different depending on the needs of the site, but is there some common 'framework' for doing so? Do people do it by hand, come up with some function that gets x from A and put it into {} as value 1, and then goes back and pick up some other context data, maybe from a different table, so it isn't a simple for loop? Or should I just use dict()? 

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/1tVDUBrPRLQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Vijay Khemlani

unread,
Feb 24, 2016, 3:42:02 PM2/24/16
to django...@googlegroups.com
Django 'is' a framework, therefore you have the flexibility to create your context as you see fit. You could call an external API, query your database, obtain results from ElasticSearch, parse another webpage, or anything that suits the needs of your particular project.

It completely depends on the domain and scope of your project.

Reply all
Reply to author
Forward
0 new messages