Permalink
Browse files

Slight modification to pjaxtend method, now has defaults for all params.

  • Loading branch information...
1 parent 0c5d15e commit 313a0ebf458dec3d0e1f39b598ccda5ee158fd18 @alexhayes alexhayes committed Mar 14, 2012
Showing with 58 additions and 8 deletions.
  1. +32 −5 README.rst
  2. +3 −3 djpjax.py
  3. +23 −0 tests.py
View
@@ -84,7 +84,7 @@ If the content in your ``template-pjax.html`` file is very similar to your
from djpjax import pjaxtend
- @pjaxtend('someapp/base.html')
+ @pjaxtend
def my_view(request):
return TemplateResponse(request, "template.html", {'my': 'context'})
@@ -94,16 +94,43 @@ Then, in your ``template.html`` file you can do the following::
...
...
-Note that the template will extend ``someapp/base.html`` unless its a pjax request
+Note that the template will extend ``base.html`` unless its a pjax request
in which case it will extend ``pjax.html``.
-If you want to define which template to extend for a pjax request you can do so
-as follows::
+If you want to define the parent for a standard http or pjax request, you can do
+so as follows::
from djpjax import pjaxtend
@pjaxtend('someapp/base.html', 'my-pjax-extension.html')
def my_view(request):
return TemplateResponse(request, "template.html", {'my': 'context'})
- Using this approach you don't need to create many ``*-pjax.html`` files.
+Using this approach you don't need to create many ``*-pjax.html`` files.
+
+If you have a collision with the variable name ``parent`` you can specify the
+context variable to use as the third parameter to pjaxtexd, as follows::
+
+ from djpjax import pjaxtend
+
+ @pjaxtend('someapp/base.html', 'my-pjax-extension.html', 'my_parent')
+ def my_view(request):
+ return TemplateResponse(request, "template.html", {'my': 'context'})
+
+Which would require the following in your template:
+
+ {% extends my_parent %}
+ ...
+ ...
+
+
+Testing
+-------
+
+Tests are run using nosetests. To install::
+
+ pip install nose
+
+And to run the tests::
+
+ nosetests tests.py
View
@@ -20,7 +20,7 @@ def _view(request, *args, **kwargs):
return _view
return pjax_decorator
-def pjaxtend(parent, pjax_parent='pjax.html'):
+def pjaxtend(parent='base.html', pjax_parent='pjax.html', context_var='parent'):
def pjaxtend_decorator(view):
@functools.wraps(view)
def _view(request, *args, **kwargs):
@@ -30,9 +30,9 @@ def _view(request, *args, **kwargs):
# warnings.warn("@pjax used with non-template-response view")
# return resp
if request.META.get('HTTP_X_PJAX', False):
- resp.context_data['parent'] = pjax_parent
+ resp.context_data[context_var] = pjax_parent
elif parent:
- resp.context_data['parent'] = parent
+ resp.context_data[context_var] = parent
return resp
return _view
return pjaxtend_decorator
View
@@ -58,6 +58,14 @@ def test_class_with_pjax_template():
resp = view(pjax_request)
assert resp.template_name[0] == "pjax.html"
+def test_pjaxtend_default():
+ resp = view_default_pjaxtend(regular_request)
+ assert resp.template_name == "template.html"
+ assert resp.context_data['parent'] == "base.html"
+ resp = view_default_pjaxtend(pjax_request)
+ assert resp.template_name == "template.html"
+ assert resp.context_data['parent'] == "pjax.html"
+
def test_pjaxtend_default_parent():
resp = view_default_parent_pjaxtend(regular_request)
assert resp.template_name == "template.html"
@@ -74,6 +82,13 @@ def test_pjaxtend_custom_parent():
assert resp.template_name == "template.html"
assert resp.context_data['parent'] == "parent-pjax.html"
+def test_pjaxtend_custom_context():
+ resp = view_custom_context_pjaxtend(regular_request)
+ assert resp.template_name == "template.html"
+ assert resp.context_data['my_parent'] == "parent.html"
+ resp = view_custom_context_pjaxtend(pjax_request)
+ assert resp.template_name == "template.html"
+ assert resp.context_data['my_parent'] == "parent-pjax.html"
# The test "views" themselves.
@@ -93,6 +108,10 @@ def view_with_pjax_template(request):
def view_with_template_tuple(request):
return TemplateResponse(request, ("template.html", "other_template.html"), {})
+@djpjax.pjaxtend()
+def view_default_pjaxtend(request):
+ return TemplateResponse(request, "template.html", {})
+
@djpjax.pjaxtend('parent.html')
def view_default_parent_pjaxtend(request):
return TemplateResponse(request, "template.html", {})
@@ -101,6 +120,10 @@ def view_default_parent_pjaxtend(request):
def view_custom_parent_pjaxtend(request):
return TemplateResponse(request, "template.html", {})
+@djpjax.pjaxtend('parent.html', 'parent-pjax.html', 'my_parent')
+def view_custom_context_pjaxtend(request):
+ return TemplateResponse(request, "template.html", {})
+
class NoPJAXTemplateVew(djpjax.PJAXResponseMixin, View):
template_name = 'template.html'

0 comments on commit 313a0eb

Please sign in to comment.