Skip to content
This repository has been archived by the owner on Nov 25, 2017. It is now read-only.

Commit

Permalink
Slight modification to pjaxtend method, now has defaults for all params.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhayes committed Mar 14, 2012
1 parent 0c5d15e commit 313a0eb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
37 changes: 32 additions & 5 deletions README.rst
Expand Up @@ -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'})

Expand All @@ -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
6 changes: 3 additions & 3 deletions djpjax.py
Expand Up @@ -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):
Expand All @@ -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
Expand Down
23 changes: 23 additions & 0 deletions tests.py
Expand Up @@ -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"
Expand All @@ -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.

Expand All @@ -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", {})
Expand All @@ -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'

Expand Down

0 comments on commit 313a0eb

Please sign in to comment.