1

I'm trying display the outputs of 2 apps on a same web page, but am encountering issues when trying to use the solution proposed here.

I have a "main_app" handling the content of most of the page, and would like to add the output of a "sub_app" (i.e. a rendered <div> element) on the same page.

Here's how I collect the output of sub_app in a main_app view:

from sub_app.views import sub_app_view #
def main_app_view(request):    
    my_sub_app_html = user_tests(request) #supposed to get rendered HTML content from sub_app
    context = {
                'sub_app_html ': my_sub_app_html,
                }
    return render(request, 'main_app.html', context)

Then the view in sub_app:

def sub_app_view(request):
    context = {
                'sub_app_context': "foo",
                }
    return render(request, 'sub_app/sub_app.html', context)

main_app.html contains:

<p>
{{ sub_app_html }}
</p>

and sub_app.html:

<div id="mydiv">
   {{ sub_app_context }}
</div>

But instead of correctly displaying the rendered HTML from sub_app_view, what is shown in main_app.html in the browser is:

<HttpResponse status_code=200, "text/html; charset=utf-8">.

Is there a solution to this, or a better way to achieve linking between apps?

sc28
  • 1,163
  • 4
  • 26
  • 48

1 Answers1

1

you can do it using **

javascript and jquery

**

$.ajax({
  type: "get",
  url: url_app_1,
  data: data,
  success: here add your html to your div1,
  dataType: dataType
});
$.ajax({
  type: "get",
  url: url_app_2,
  data: data,
  success: here add your html to your div2,
  dataType: dataType
});

and in the success you can append the html to your div

**

Django solution:

**

The render function return an HttpResponse(content, content_type, status) so to get the template html you need to:

def main_app_view(request):    
    my_sub_app_html = user_tests(request) 
    context = {
                # you need to get content then decode
                'sub_app_html ': my_sub_app_html.content.decode("utf-8"),
                }
    return render(request, 'main_app.html', context)

Here is the source code of the render

idirall22
  • 316
  • 2
  • 11
  • Ah thanks, I was hoping for a "pure Django" solution, but this can do as well actually. For the sake of learning though, do you have any explanation why my approach was leading to the `` object instead of the rendered `HTML`? – sc28 Mar 20 '19 at 09:33
  • Much clearer, thanks! For the "Django solution", I had to add `{{ sub_app_context|safe }}` because some symbols were [apparently](https://stackoverflow.com/a/275246/3976696) wrongly interpreted, and I had to add `window.onload = function() {...}` on some JS scripts contained in the submitted context to ensure they were properly applied (this wasn't required with the "Jquery solution"). I really appreciate the luxury to choose between both solutions, however do you have any recommendations as to which is preferable (in terms of safety, maintainability, speed...), or are both equivalent? – sc28 Mar 21 '19 at 21:35