Skip to main content

Django render_partial tag allows inserting rendered views into templates

Project description

Installation

  1. pip install django_render_partial

  2. Add 'django_render_partial' to INSTALLED_APPS

  3. Ensure that 'django.template.context_processors.request' is in TEMPLATES['OPTIONS']['context_processors']

Usage

Write a template for partial view, e.g. partial_view.html:

<p>{{ arg1 }} + {{ arg2 }} = {{ result }}</p>

Write a partial view:

def partial_view(request, *args, **kwargs):
    result = kwargs['arg1'] + kwargs['arg2']
    kwargs['result'] = result
    return render(request, 'partial_view.html', kwargs)

if you are using function-based views, or:

class PartialView(TemplateView):
    template_name = 'partial_view.html'

    def get_context_data(self, **kwargs):
        result = kwargs['arg1'] + kwargs['arg2']
        kwargs['result'] = result
        return super(PartialView, self).get_context_data(**kwargs)

if you are using class-based views.

Add it to urls.py:

url(r'^partial-view/(?P<arg1>\w+)/(?P<arg2>\w+)/$',
    partial_view,
    name='partial_view'),

or:

url(r'^partial-view/(?P<arg1>\w+)/(?P<arg2>\w+)/$',
    PartialView.as_view(),
    name='partial_view'),

In your template:

{% load render_partial %}
{% with some_var=2 %}
  {% render_partial 'partial_view' arg1=40 arg2=some_var %}
{% endwith %}

The render_partial tag would be rendered to:

<p>40 + 2 = 42</p>

Note that the with tag above is not required for render_partial to work. It is used to show that render_partial accepts variables. Every argument will be evaluated against context except for the names of any keyword arguments.

If you don’t want to expose your partial view in urls.py, you can also use fully qualified dot separated view name:

{% render_partial 'partial_test.views.PartialView' arg1=40 arg2=some_var %}
{% render_partial 'partial_test.views.partial_view' arg1=40 arg2=some_var %}

IMPORTANT: the calling template must receive a context variable called request containing the original HttpRequest. Don’t forget to add 'django.template.context_processors.request' to TEMPLATES['OPTIONS']['context_processors'].

Adapted from https://djangosnippets.org/snippets/1568/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-render-partial-0.3.zip (7.4 kB view details)

Uploaded Source

File details

Details for the file django-render-partial-0.3.zip.

File metadata

File hashes

Hashes for django-render-partial-0.3.zip
Algorithm Hash digest
SHA256 3d51cef309f436969b256ce4377fce9d5ee4f3b7dd0358a39e77b8418cd556a4
MD5 bf13e47c010cb9c6c3257c8532deb8da
BLAKE2b-256 8dfb5af470ffe25f8e75a73a6de55e034d6ebca3fd77a50b713c08219f5eb65f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page