[Answered ]-Merge different views in one django template

2👍

It would be good if you were more clear in your question, as I think I do not fully understand what you want. But if I do, here are a couple options:
1) If you can separate your logic into some functions, do it. Then you can call those functions from your third view, get your template args and render the template with everything you want.
2) You could also load this two templates separately on the same page with ajax.

There could be many other options, it really depends on what you’re trying to get in the end (which is not clear).

0👍

If you want to combine the same forms in one template from one view:

from django.shortcuts import render

@login_required
@csrf_protect
def viewone(request):

   ctx = {} # context dict
   if request.method == 'GET':
     # code here
     form1 = MyForm(request.GET)
     ctx['form1'] = form1
     return render(request,'template.html',ctx)

   if request.method == 'POST':
     form = ViewOne(request.POST)
     if form.is_valid():
         # code here
         return render(request,'some.html')
     else:
        form = ViewOne()
        ctx['form'] = form
        return render(request,'template.html',ctx)

Then in your template.html:

{% if form %}
   {{ form }}
{% endif %}

{% if form1 %}
   {{ form1 }}
{% endif %}

Leave a comment