Commits

James Bennett committed e0b1022

Add support for extra_context in views

Comments (0)

Files changed (2)

 
 **Optional arguments:**
 
+``extra_context``
+    A dictionary of variables to add to the template context. Any
+    callable object in this dictionary will be called to produce the
+    end result which appears in the context.
+
 ``form_class``
     The form class to use for validating and creating the user
     profile. This form class must define a method named ``save()``,
 
 **Optional arguments:**
 
+``extra_context``
+    A dictionary of variables to add to the template context. Any
+    callable object in this dictionary will be called to produce the
+    end result which appears in the context.
+
 ``form_class``
     The form class to use for validating and editing the user
     profile. This form class must operate similarly to a standard
 
 **Optional arguments:**
 
+``extra_context``
+    A dictionary of variables to add to the template context. Any
+    callable object in this dictionary will be called to produce the
+    end result which appears in the context.
+
 ``public_profile_field``
     The name of a ``BooleanField`` on the profile model; if the value
     of that field on the user's profile is ``False``, the ``profile``

profiles/views.py

 
 
 def create_profile(request, form_class=None, success_url=None,
-                   template_name='profiles/create_profile.html'):
+                   template_name='profiles/create_profile.html',
+                   extra_context=None):
     """
     Create a profile for the current user, if one doesn't already
     exist.
     
     **Optional arguments:**
     
+    ``extra_context``
+        A dictionary of variables to add to the template context. Any
+        callable object in this dictionary will be called to produce
+        the end result which appears in the context.
+
     ``form_class``
         The form class to use for validating and creating the user
         profile. This form class must define a method named
             return HttpResponseRedirect(success_url)
     else:
         form = form_class()
+    
+    if extra_context is None:
+        extra_context = {}
+    context = RequestContext(request)
+    for key, value in extra_context.items():
+        context[key] = callable(value) and value() or value
+    
     return render_to_response(template_name,
                               { 'form': form },
-                              context_instance=RequestContext(request))
+                              context_instance=context)
 create_profile = login_required(create_profile)
 
 def edit_profile(request, form_class=None, success_url=None,
-                 template_name='profiles/edit_profile.html'):
+                 template_name='profiles/edit_profile.html',
+                 extra_context=None):
     """
     Edit the current user's profile.
     
     
     **Optional arguments:**
     
+    ``extra_context``
+        A dictionary of variables to add to the template context. Any
+        callable object in this dictionary will be called to produce
+        the end result which appears in the context.
+
     ``form_class``
         The form class to use for validating and editing the user
         profile. This form class must operate similarly to a standard
             return HttpResponseRedirect(success_url)
     else:
         form = form_class(instance=profile_obj)
+    
+    if extra_context is None:
+        extra_context = {}
+    context = RequestContext(request)
+    for key, value in extra_context.items():
+        context[key] = callable(value) and value() or value
+    
     return render_to_response(template_name,
                               { 'form': form,
                                 'profile': profile_obj, },
-                              context_instance=RequestContext(request))
+                              context_instance=context)
 edit_profile = login_required(edit_profile)
 
 def profile_detail(request, username, public_profile_field=None,
-                   template_name='profiles/profile_detail.html'):
+                   template_name='profiles/profile_detail.html',
+                   extra_context=None):
     """
     Detail view of a user's profile.
     
     
     **Optional arguments:**
 
+    ``extra_context``
+        A dictionary of variables to add to the template context. Any
+        callable object in this dictionary will be called to produce
+        the end result which appears in the context.
+
     ``public_profile_field``
         The name of a ``BooleanField`` on the profile model; if the
         value of that field on the user's profile is ``False``, the
     if public_profile_field is not None and \
        not getattr(profile_obj, public_profile_field):
         profile_obj = None
+    
+    if extra_context is None:
+        extra_context = {}
+    context = RequestContext(request)
+    for key, value in extra_context.items():
+        context[key] = callable(value) and value() or value
+    
     return render_to_response(template_name,
                               { 'profile': profile_obj },
-                              context_instance=RequestContext(request))
+                              context_instance=context)
 
 def profile_list(request, public_profile_field=None,
                  template_name='profiles/profile_list.html', **kwargs):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.