Can Django Sub Views be displayed on a Single Web Page

I have been thinking, is possible to have a view and have sub views off it, so that more than one view can be displayed on a single web page ?

What I am trying to do, is to have a Django reference section similar to what W3Schools Django References has.

I have Links to different reference sections (eg Template Tags Reference, Filter Reference etc) on my main home page, as can be seen below.

This post does not allow me to show another screenshot, but when Template Tag Ref Page Link is clicked, it is meant to display results of all template tags on a /template/ route.
At the moment to keep it working I have shown autoescape off and this is what gets displayed on localhost template/ route below.
I have not been able to get autoescape on displayed below this example, because it has same view.

Django Template Tag Reference

autoescape Template Tag

Autoescape Off

Hello my World!

Check out views.py to see what the heading variable looks like

I have paths setup in urls.py, like this

# Add Routes for Django References Section
    # Template Tag Reference 
    path('template/', views.template, name='template'),
    
    # Filter Reference
    path('filter/', views.filter, name='filter'),
    
    # Field Lookups Reference
    path('field/', views.field, name='field'),

This a Template Page view I have setup in my views.py

# Django References Section (57 to 59)

# Django Template Tag Reference 57 within Django References Section

# Using Keyword Autoescape
# Autoescape Off
#def template_autoescape_off(request):
def template(request):
  template = loader.get_template('template_tag_ref.html')
  context = {
    'heading': 'Hello <i>my</i> World!',
  }
  return HttpResponse(template.render(context, request))

Note: I was trying to use def template_autoescape_off(request): as a sub view, but because that did not work, I have commented it out.

I have also commented out template view for autoescape on as shown in code below, because it did not work with same template view.

# Autoescape On
#def template(request):
  #template = loader.get_template('template_tag_ref.html')
  #context = {
    #'heading': 'Hello <i>my</i> World!',
  #}
  #return HttpResponse(template.render(context, request))

I thought that having all tag references displayed on one page, even though it is long, would cut down on having heaps of routes.

I am also thinking on my localhost home page, if you click on Template Tag Reference and it takes you to localhost /template/ route, all Links could be displayed for each of the tags.

This would be similar to what W3Schools has, but a lot of routes would be needed like /ref_tags_autoescape/ etc and some of these references have more than one example, so they would need more than one view in the page.

I am trying to get some advice on the best way to approach this, so that it works efficiently for me, thanks.

The effect that you want to achieve can be done, yes. But not the way you’re thinking of doing it.

To directly answer your question, no. You don’t organize your code such that you’re trying to call multiple views for a page.

You want to change your mindset and your mental perspective for this.

Looking at the fundamentals:

  • A URL calls a view.

  • A view retrieves data to create a dict called a context

    • There are modules known as context processors that are called during the rendering process to add additional data to the context. These context processors are called during every template being rendered and are useful for adding data that needs to be supplied for every page.
  • The view calls a rendering function (e.g. render) with a template name and the context.

  • The rendering function converts that template to an HTML page.

    • The template being rendered can extend a parent template. (That parent template itself could extend yet another template.)
    • The template being rendered can also include other templates. (Those templates being included can likewise include more templates.)

So, in the general case, when you want to add something like a menu to every page, you would write a context processor to add the data for the menu to the context.

You would then add the template code to render that menu to your base template.

It can get more sophisticated than that. Since the context processor receives the original request, it can customize the context data being returned based upon the user making the request. This allows you to customize a menu based upon a user’s permissions.

Hi Ken

Thanks for the reply

I am just trying to get an understanding on how to do this with a context processor you have mentioned.
It seems to me that this context processor you mentioned can be used for handling several pages with similar layout.

I have been thinking about the best way I can do this Django Reference Section that Django Template Tags Reference has.

I think for the different sections like Template Tag Reference, Filter Reference and Field Lookups Reference, when you click on them the best way to have this should be to display Links for each of the Tags eg autoescape etc.
This is because when you click that link you can look at each individual tag eg Autoescape, separately rather than have a whole heap of tags on the one page with a template/ route, like I mentioned before.

I am trying to get my head around this.
It seems like all these tag pages for example autoescape Template Tag, Block Template Tag etc can have a parent template for the layout and use a child template as well.

Template Tag Reference page has a template/ route
and a tag found from clicking on a link eg autoescape tag can have a ref_tags_autoescape/ route that is controlled by a context processor, so you do not have to create a whole heap of separate routes.
Need to have some way of controlling these routes for each of the tags for Template Tag Reference, Filter Reference and Field Lookups Reference Sections.

I am going away on holiday in a weeks time, but just want to trying get as good of an understanding on this before I go.