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

Commit

Permalink
Merge pull request #6 from alexhayes/master
Browse files Browse the repository at this point in the history
pjaxtend - alternative usage
  • Loading branch information
jacobian committed Mar 14, 2012
2 parents eaf83ba + a02f0f0 commit 1f7f69c
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
62 changes: 61 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,64 @@ if you want a specific template used for PJAX responses::
def get(self, request):
return self.render_to_response({'my': 'context'})

That's it!
That's it!

Using Template Extensions
-------------------------

If the content in your ``template-pjax.html`` file is very similar to your
``template.html`` an alternative method of operation is to use the decorator
``pjaxtend``, as follows::

from djpjax import pjaxtend
@pjaxtend
def my_view(request):
return TemplateResponse(request, "template.html", {'my': 'context'})

Then, in your ``template.html`` file you can do the following::

{% extends parent %}
...
...

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 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.

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
16 changes: 16 additions & 0 deletions djpjax.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ def _view(request, *args, **kwargs):
return _view
return pjax_decorator

def pjaxtend(parent='base.html', pjax_parent='pjax.html', context_var='parent'):
def pjaxtend_decorator(view):
@functools.wraps(view)
def _view(request, *args, **kwargs):
resp = view(request, *args, **kwargs)
# this is lame. what else though?
# if not hasattr(resp, "is_rendered"):
# warnings.warn("@pjax used with non-template-response view")
# return resp
if request.META.get('HTTP_X_PJAX', False):
resp.context_data[context_var] = pjax_parent
elif parent:
resp.context_data[context_var] = parent
return resp
return _view
return pjaxtend_decorator

class PJAXResponseMixin(TemplateResponseMixin):

Expand Down
48 changes: 48 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,38 @@ 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"
assert resp.context_data['parent'] == "parent.html"
resp = view_default_parent_pjaxtend(pjax_request)
assert resp.template_name == "template.html"
assert resp.context_data['parent'] == "pjax.html"

def test_pjaxtend_custom_parent():
resp = view_custom_parent_pjaxtend(regular_request)
assert resp.template_name == "template.html"
assert resp.context_data['parent'] == "parent.html"
resp = view_custom_parent_pjaxtend(pjax_request)
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.

@djpjax.pjax()
Expand All @@ -76,6 +108,22 @@ 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", {})

@djpjax.pjaxtend('parent.html', 'parent-pjax.html')
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 1f7f69c

Please sign in to comment.