1

I want to insert another view into a view.

Basically I'm using Django filter package and value output directly populates template without my need to writing any view class.

I want to insert another html into the current template. Something like this: {% include 'countsnippet' %}, but this gives me error template doesn't exists.

Alternatively I've tried using {% include 'countsnippet.html' %} but this doesn't calls the views and fields remains empty.

What approach I can use to include another view?

This is my countsnippet html.

{% block content %}
<p> INFT : {{ inft }} </p>
{% endblock %}

I've already made view for countsnippet and its url looks like this:

url(r'countsnippet/$', countsnippet, name='countsnippet')

This is snippet for countsnippet view:

 def countsnippet(request):
     checkins = Attendee.objects.filter(checkin=True)
     inft = Attendee.objects.filter(branch='INFT')
     cmpn = Attendee.objects.filter(branch='CMPN')
     mms = Attendee.objects.filter(branch='MMS')
     return render( request, 'search/countsnippet.html', {'inft': 
 (checkins&inft).count(), 'cmpn': (checkins&cmpn).count(), 'mms': 
 (checkins&mms).count()} )
alter123
  • 601
  • 2
  • 11
  • 32
  • I'm pretty sure between using extend, include, and block sections you could accomplish what you are looking for. – dfundako Aug 14 '18 at 13:27
  • I've tried with include and block systems, but I'm not able to figure out a way. I've also tried implementing as mentioned here ( https://stackoverflow.com/questions/10608114/django-two-views-one-page ) but I couldn't able to find a way which fits the way I want. – alter123 Aug 14 '18 at 13:36
  • List for me, please, what variables should be declared in context of `countsnippet.html` – Pavel Minenkov Aug 14 '18 at 13:38
  • Sure, view of count snippet returns a dictionary which contains couple of values as follows: `return render( request, 'countsnippet.html', {'inft': inft.count(), 'cmpn': cmpn.count(), 'extc': extc.count() }) ` and my template snippet is mentioned in question. Thanks – alter123 Aug 14 '18 at 13:43
  • Are you sure` {'inft': inft.count(), 'cmpn': cmpn.count(), 'extc': extc.count() }` are declared IN parent view? – Pavel Minenkov Aug 14 '18 at 14:15
  • I've tried loading just the codesnippet view and everything worked fine out there. But they're not mentioned in parent view, because I dont have access to the parent 'view' itself – alter123 Aug 14 '18 at 14:17

1 Answers1

3

You cannot add a view directly into a template since it will want a request and a context. In my opinion to solve your problem you have two different options:

1 Use inclusions tags

You can use inclusion tags to integrate a template with context into another template, see django docs: https://docs.djangoproject.com/en/2.1/howto/custom-template-tags/#inclusion-tags

2. Use a view included as the context of the wrapper view inside the view function/class

You can include your "view", in this case a generated template with context, directly into the context dictionnary of your wrapper view. You could achieve this by doing something similar to this:

# views.py
from django.views.generic import TemplateView
from django.template import Context, Template

# Wrapper view
class WrapperView(TemplateView):
    """
    This is the wrapper view, you include the inline view inside the
    wrapper view get_context_data.
    """
    template_name = "wrapper_template.html"

    def get_context_data(self, **kwargs):
        context = super(WrapperView, self).get_context_data(**kwargs)

        inline_context = {
            'name': 'Steve'
        }
        inline_html_template = Template('inline_template.html')
        inline_view = html_template.render(Context(inline_context))

        context['inline_view'] = inline_view

        # In the wrapper template you can show the html of your inline_view with {{ inline_html|safe }}

        return context

Otherwise you can generate a view from a context like this, but it will want a request object: InlineView.as_view(add_context=context)(request) and I am not exactly sure how you would embed it in your other view afterwise.

Maxime Deuse
  • 313
  • 11
  • 13
  • I'm not sure weather I've understood what you mentioned or not, but I'm not able to figure out where I'm adding the data for my current parent class. It will be good if you can help me figuring out that. – alter123 Aug 14 '18 at 14:21
  • You can add the data in the inline_context dictionary in the case of this example, it will be passed to the html_template through the render function. – Maxime Deuse Aug 15 '18 at 18:00
  • @jayVasant, I updated the question with another possibility, which may be simpler to implement for your needs. – Maxime Deuse Aug 21 '18 at 07:27
  • But `Template('inline_template.html')` does not do what you mean here, I suppose. Maybe you mean `get_template('inline_template.html')`? – nerdoc Jan 22 '22 at 20:20