Django Passing list of objects to template


I have trouble of passing my get_profiles in the same template as r'^compose/$' here. r'^users/$' is what I'm using as a model and it works. "compose" is a function in my views.py.

from django.conf.urls.defaults import * from django.views.generic.simple import redirect_to from django.views.generic.simple import direct_to_template from messages.views import * from userprofile.views import get_profiles urlpatterns = patterns('', url(r'^$', redirect_to, {'url': 'inbox/'}), url(r'^inbox/$', inbox, name='messages_inbox'), url(r'^outbox/$', outbox, name='messages_outbox'), url(r'^compose/$', compose, name='messages_compose'), url(r'^users/$', direct_to_template, {'extra_context': { 'profiles': get_profiles }, 'template': 'messages/users.html' }), ) userprofile/views.py def get_profiles(): return Profile.objects.order_by("user")

I tried this:

url(r'^compose/$', compose, direct_to_template, {'extra_context': { 'profiles': get_profiles }, 'template': 'messages/compose.html' }),

But I get a function object is not iterable.


As others have said, you need to actually call the function, but if you do that in urls.py it will only be evaluated once per process. You don't want to do that.

You don't show what get_profiles does, but I assume it's some sort of utility function. I tend to think that those belong in a separate file, lib.py or utils.py, rather than views.py. (That is, assuming it's not actually a view itself - if it is, then you'll need to rethink your whole approach).

However, what I think you actually need to do is to make a template tag instead. You can keep the logic in get_profiles if you like, then make a simple tag that calls that function. Then you don't need to mess about with passing data in extra_context - just add the tag to your template.


Try adding brackets to the function call maybe?

'profiles': get_profiles()

Otherwise you are just passing a reference to the function object.

But the problem is this would only be evaluated once, when urls.py is called.

Why not make a view function to correspond with the url 'users/'?


get_profiles(), with parentheses


  • Rails 3.2, No Route Matches { :controller=>'xxx' :action=>'xxx\" }
  • iOS Swift Email Deep Linking
  • :dependent => :destroy isn't calling the destroy method before the deletion?
  • Django Passing list of objects to template
  • Associated models and a nested form with validation not working
  • How do I set up acts as votable to not require user sign in?
  • Jmeter - get parameter from redirect URL
  • Redirect and raise flash message when catch-all in routes
  • How do you respond_to another js file in the controller using Ruby on Rails?
  • undefined method `microposts'
  • Render a form after form submission
  • how validation is done using wicked?
  • LoadError in OmniauthCallbacksController#passthru (with devise/stripe connect)
  • How to pass hidden parameter to controller in Rails3
  • Setting up logging out…is there a way to check if a controller action contains a before_action
  • How to display a selection based on user input using ajax and jquery
  • Rails remote => true confusion (no format.js?)
  • Getting Error like imap_open(): Couldn't open stream in server
  • get sent mails via imap
  • Intercept incoming SMS
  • How to show AccessDenied errors on the active page with CanCan in Rails3
  • Rails: form submission isn't properly creating attributes?
  • Is there a simple MVC4 html helper for collapsible (multi-level) Bootstrap 3 menus?
  • Prevent users from voting multiple times in Rails
  • How can I mark as read all emails fetched through EWS in a single update?
  • Calling a controller from another
  • Matching multiple strings to one long string using patterns
  • Django how to modify database records by template
  • Conditional serialization with protobuf-net
  • How does extglob work with shell parameter expansion?
  • Rails 3, Custom Actions, and HTML request methods
  • Skip Characters in Oracle TO_DATE function
  • Implementation of RTTI using typeid
  • How to access recipient on sent messages page with mailboxer
  • Spring security and special characters
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Circular dependency while pushing http interceptor
  • AngularJs get employee from factory
  • How to set the response of a form post action to a iframe source?
  • Change div Background jquery