Call 1 app from another in Django and fails.

Django version 5.1.5
HTMX 2.0

Setup:
Django project has 2 apps:
console_app - this app initially calls a view.py function console_view and displays a page named base_console_ap.html. This page is basically a dashboard. Along 1 side of this dashboard is a summary list of turnover_log entries.

turnover_app - this app initially calls a view.py function turnover_index and displays a page named index.html. This app basically allows the creation and editing of turnover_log entries.

Goal:
To select a turnover_log entry from dashboard (console_app) summary and have it go to the turnover_app function turnover_index and load the index.html. page. Ultimately pass the record id(pk) and load that distinct record selected from the dashboard. But I cannot even get it to load the static index.html page properly, so until I can do that no reason to add complexity of passing a pk value.

What happens:
If I use:

<a href="{% url 'turnover_app:turnover_index' %}">TurnOver Log</a>

works fine loads index.html page all good. But of course does not pass a value (pk) my ultimate goal. just a test here.

If I use:

       <button type="button" id="btn" 
          class="btn btn-outline-success"
          hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
          hx-get="{% url 'turnover_app:turnover_index' %}">
          TurnOver Log
        </button>

it does call the view.py function turnover_index and loads the index.html page . What happens is the current console_app (dashboard) base_console_ap.html. stays loaded and visible and the other page turnover_app (index.html) page loads over and under around the current page (base_console_ap.html). of course it’s all messed up.

Remember the code above is just test code to see if I can load the static index.html page from another app. Not trying to pass (pk) value yet. I’m sure I’m missing something stupid but that’s learning.
I did put entries in both urls.py apps reference both ways.

console_app/urls.py (partial)

path('turnover_index/', views.turnover_index, name='turnover_index'),

turnover_app/urls.py (partial)

path('turnover_index/', views.turnover_index, name='turnover_index'),

NOTE:
The reason I’m trying to use an HTMX control is to use hx-include to maybe pass a value. I would like the option of using for example something like myvar = request.GET.get(‘my_val’) back to an HTML entity in another app, if that’s even possible? even tried having a function in console_app call the turnover_index function in turnover_app and still the same results.

As usual all support helps and I’m thankful.

You want to change your mindset and mental model regarding this. You are not “calling an app from another app”.

In all cases here, you have a view that generates a response.

That response is sent out to the browser.

The response contains links or references to other URLs.

A request made to one of those URLs results in another view being called. The app that that view resides in is not important or relevant to anything being discussed here. There is no direct connection between the two events.

Just curious here: Why do you have 2 apps?

When using HTMX you do not want to render and respond with a complete page. The appropriate response generated by a view accessed by an HTMX-based request is the <div> element being replaced. Rendering and returning a complete page is the wrong thing to do here. (I would suggest you review the docs and examples at </> htmx ~ Documentation for more detailed information.)

This is, at best, unnecessary and at worst, potentially problematic.

Regarding the creation and reference to views with passing a parameter value, review the work you would have done in the Django tutorial. It covers both the process of defining views that accept parameters along with creating url references to those views. (Writing your first Django app, part 3 | Django documentation | Django)

Thank you Ken for your response. I will address your questions / statements.

1.) You want to change your mindset and mental model regarding this. You are not “calling an app from another app”.

Since my early days of programming and the inception of Subroutines and Functions the terms “called, calling” has been used in most languages, python included to this day. Reflexive I guess. I do understand the MVT Django process.

Django’s documentation says this.
“In Django, web pages and other content are delivered by views. Each view is represented by a Python function (or method, in the case of class-based views). Django will choose a view by examining the URL that’s requested (to be precise, the part of the URL after the domain name).”

The consensus with Django would be “delivered (called)” and “request / requested(calling)”

2.) Just curious here: Why do you have 2 apps?

Because they do very different things:

Turnover_app - manages(crud) all information we use in detail in our turnover log day to day. Also has in depth API access to our systems where applicable.

Console_app - Is a dashboard. This dashboard is a view into many critical things in our NOC. It gathers a lot of information (API’s) from our many systems. Industry term is “Connector”. One of the other items is a quick view (summary) of turnover log entries. Both of these apps work great with all these moving parts. The request has been "allow the selection of(ticket (int)) of an entry listed in the turnover log quick view and have it request the turnover_app (view) to be delivered to the display with the selected entry(ticket) ready for editing. The user at that time can stay in the turnover_app and perform other needed crud actions.

3.) This is, at best, unnecessary and at worst, potentially problematic.

console_app/urls.py (partial)

path('turnover_index/', views.turnover_index, name='turnover_index'),

turnover_app/urls.py (partial)

path('turnover_index/', views.turnover_index, name='turnover_index'),

Without both of these entries in the urls.py files Django tosses an error. That error goes away with both of them. Now this maybe wrong(I’m sure it probably is) but that’s why I’m here to learn from the guys who know this.

Bottomline: I’m just trying to do what I stated above. Select a ticket from the turnover log quick view from the dashboard summary table and have it request / load / view(turnover_index) the turnover_app for crud actions.

I have made many requests for apps to load / view but that has only been thru the standard href.
i.e.:
eNOC Dashboard

I have requested views from other apps to use those views in my currently viewed app. I have not made a request into another app’s views to replace the current apps view with that app in something other than using href i.e.: HTMX. Please forgive my ignorance and I’m sure I’m missing a lot as well as full / proper syntax etc.

With all my programming I tend to make small POC’s along the way to my final desired code action if it’s well involved. In this instance
I test my code to request a currently working view’s (only requests) to see if that works. Then move forward and test a view with added data i.e.: pk value etc.

In this case I’m only requesting another app to be viewed. I can see it’s making a request into the correct view(function) but when it gets to the return render(request, template, context) it overlays and intermingles the new html page with the current viewed page.

To clarify my point - one app is not calling the other app, nor is it requesting data from that other app. There is no direct flow of control or passing of data between the two. The two views are not connected.

  • Step 1: A view in App 1 delivers a page to the browser. At this point, the view is out of scope. It is a function that has returned and has no more state.
  • Step 2: The browser renders the page.
  • Step 3: The user does something on the page that causes the browser to issue a request for a URL.
  • Step 4: Django calls the view associated with that URL. That view then returns a response to the browser. This would work exactly the same as if you were to issue that request directly in your browser’s address bar. There is no “reference”, “association”, “control” or “influence” relative to the first view. There is no connection between this and the earlier request.

It would be helpful to see the full error with traceback. Having both of these as you’re showing here is an indication of something else being wrong.

Again - to be clear, the apps are not connected. That these views exist in different apps is immaterial. You are only creating a potential point of confusion by trying assign any relevance to that aspect of your project’s structure.

Yes, because you’re trying to return a full page. With using HTMX, you only want to render and return the desired div.

In the common case, this involves either creating a custom view and template for that fragment, or using one of the available libraries or techniques to render the complete page and then extract the desired portion. (e.g. See Using HTMX and django-template-partials together)

Side note: This, by itself, would not be considered in many circles to be a sufficient reason to split this into separate apps. Do you have a known intent to split these and reuse them in separate projects? If so, then yes, absolutely split them out.
(The console_app does seem like a likely candidate for this)

Thank you so much for all the information and of course your time.
I will work on understanding this better. One thing I learned / reminded of by you is HTMX is more for loading partials which I do a lot of instead of entire pages. I guess I did not get this as I should of. Yes there will be much more to this project as time passes and the intent to split things out. Thank you.