Skip to content
This repository was archived by the owner on Nov 15, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ If you want to render the included view only with the variables provided (or eve
{% view 'myapp.views.MyView' with foo='bar' only %}
```

### View keyword arguments

If your view requires kwargs in the URL, such as a pattern like

```python
url_patterns = [
path("item/<pk:pk>/edit", ItemEditView.as_view(), name="item-edit-view"),
]
```

you can supply these in the template tag directly after the import string and before the `with` keyword:

```html+django
{% view 'myapp.views.ItemEditView' pk=pk with extra_food="spam" %}
```

or without any extra context variables:

```html+django
{% view 'myapp.views.ItemEditView' pk=pk %}
```

> These kwargs are the ones passed to the view's `setup()`, not to the `__init__` method

## Using the `viewblock` tag

The `{% viewblock %}` tag renders a class based view and includes the content in the current template, but provides a block for additional nodes which are rendered first and made available in the included view’s context.
Expand Down
18 changes: 16 additions & 2 deletions django_view_composer/templatetags/view_composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ def parse_view_tag(parser, token):
options["no_parent_ctx"] = True
options["vars"] = token_kwargs(arg_group[2], parser)

# kwargs for the view should preceed the 'with' bit
arg_group[0].pop(0)
if len(arg_group[0]) > 0:
options["kwargs"] = token_kwargs(arg_group[0], parser)

return options


Expand Down Expand Up @@ -79,6 +84,12 @@ def render(self, context):
for k in self.options["vars"]:
child_context[k] = self.options["vars"][k].resolve(context)

# resolve the kwargs
resolved_kwargs = {}
if "kwargs" in self.options:
for k in self.options["kwargs"]:
resolved_kwargs[k] = self.options["kwargs"][k].resolve(context)

# render any nodes in the block first and add these to the child
# view context
if self.nodelist:
Expand All @@ -87,12 +98,15 @@ def render(self, context):
# render the view to a response
instance = view_class(request=request, extra_context=child_context)

# call setup on the view
instance.setup(request, **resolved_kwargs)

# if the view class has a compose method use that, otherwise
# default to get method
if hasattr(instance, "compose"):
response = instance.compose(request)
response = instance.compose(request, **resolved_kwargs)
else:
response = instance.get(request)
response = instance.get(request, **resolved_kwargs)

# only render if there is something to render
if hasattr(response, "render"):
Expand Down
23 changes: 22 additions & 1 deletion tests/basic/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,25 @@ def test_nested_block_views(self):
{"food": "spam"},
)
test_ctx = jsonpickle.decode(res)
self.assertEqual(test_ctx["food"], "spam")
self.assertEqual(test_ctx["food"], "spam")

def test_view_kwargs(self):
res = self.get_with_context(
"{% load view_composer %}"
"{% view 'basic.views.KwargsTestView' food='spam' with ham='eggs' %}",
{},
)
test_ctx = jsonpickle.decode(res)
self.assertEqual(test_ctx["food_kwarg"], "spam")
self.assertEqual(test_ctx["ham"], "eggs")

def test_viewblock_kwargs(self):
res = self.get_with_context(
"{% load view_composer %}"
"{% viewblock 'basic.views.KwargsTestView' food='spam' with ham='eggs' %}"
"{% endviewblock %}",
{},
)
test_ctx = jsonpickle.decode(res)
self.assertEqual(test_ctx["food_kwarg"], "spam")
self.assertEqual(test_ctx["ham"], "eggs")
11 changes: 9 additions & 2 deletions tests/basic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ class ContextTestView(TemplateView):
template_name = "basic/context.html"

def get_context_json(self):
context = self.get_context_data()
context = self.get_context_data(**self.kwargs)
return jsonpickle.encode(context)


class BlockTestView(TemplateView):
template_name = "basic/block.html"
template_name = "basic/block.html"


class KwargsTestView(ContextTestView):
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["food_kwarg"] = kwargs["food"]
return context