0

Say I have a django app called App1 and a django app called App2.

App1 has an endpoint called getJson() which returns a json object.

Now, in App2, I have an endpoint which renders an html template. In the html template I have a button and when the button is clicked, I want to call App1's getJson function. Is there a better way to do this than doing a get request in the JS of the template? If so, how?

Thanks!

1 Answer 1

0

Short answer, no. That's the basic flow of a Django App, you get requests and you respond.

The Django apps are running on a server machine. When you say App2 renders an html template, what it is doing is sending an HttpResponse with the given template, rendered, as payload to whoever requested it. This will be received in another machine, by another process.

So, if that other process (in another machine or the same) renders the text as an html, loads a website and shows a button, its not at all linked to your Django App. App2 ends its job when it renders and sends an HttpResponse. So if you want to control what ever happens when you press that button, you will need to communicate back with your server. And we do that sending http requests.

3
  • Hi, thanks for your response. So, just to confirm, I would have to make an http request in the html of App2 where I call mywebsite.com/app1/getJson like this? Is there any other way to do it? Thanks!
    – user12989805
    Commented Apr 29, 2020 at 14:42
  • That's the way to do it! What url you call is defined in the urls.py file. The Apps modularization is just a backend structure, the frontend only requests stuff to an URL. Commented Apr 29, 2020 at 15:47
  • I see. Thanks so much!
    – user12989805
    Commented Apr 29, 2020 at 16:14