building mutipage sphinx book into django app

Hello there!

I’m trying to build an interactive book. Currently I’ve built a webpage in django with a user registration system. I’ve also built a basic book using sphinx and the pydata-sphinx-theme.

Now my problem is the following, when I ‘make’ the html files from my markdown pages in sphinx I get 10+ html files, each file containing (hardcoded) cross references to one of the other files. I can probably change the links to dynamic links by creating sphinx templates, but how do I import all these files in django? Should I create a django view for every html page from sphinx ? Or is there possibly some jinja magic I could perform ? all html pages should live in one singe app (e.g.: /book/ch1.html#par1, /book/ch1.html#parX, /book/ch2.html#par1, /book/chX.html#parX, etc), so there probably would be some smart way of doing this ?

Also, I have failed to find any tutorials on building sphinx into django, however there are numerous websites online who function by doing this (I suspect even the documentation page of django uses sphinx). Am I maybe using the wrong google search terms ?

I hope anyone can point me in the right direction here?

Currently my django views looks like this:

from django.shortcuts import render
from django.contrib.auth.decorators import login_required

# Create your views here.
def home(request):

    context = {
        'title': 'Book'

    return render(request, 'book/home.html', context)

def statics(request):

    context = {
        'title': 'Statics'
    #How to import 10 chapters of sphinx html documents here ?
    return render(request, 'book/statics.html', context)

and one of the example sphinx files (a placeholder page) looks like this (please note the link list of links in the sidebar at the bottom of the html as well):

tutorial.html (39.3 KB)

What sphinx is actually creating for you is a static site. Since it’s creating the full html for you, with links to the other pages, Django isn’t needed to serve these docs.

You can treat these static pages as “external-ish” to your main site. Set up a base for the url that is separate from all the other urls on your site and configure your server to map your created pages to that url. (For example, you might have static/ as your STATIC_URL, where /static/ is mapped to some physical directory on your server for nginx to serve. You could then define /book/ as being mapped to whatever directory contains your docs.)

Hi there!

Thanx for the reply!

That might work, only how do I then use decorators so I can make this site only accessible for people that have logged in? Do I just put the html form the static site in the static directory and combine it with the dynamic part of the navbar ?

For now my solution would be to write some automation that extracts the required parts from the head and body of the sphinx files, slap some jinja tags around it, replace the links in the body and copy the files to the static folder in djago.

Seems like a bit tedious but i guess that’s how it is.

Thank you!

If you have the need to protect and secure static files, whether they are your documentation or other static resources, and prevent people who aren’t logged in from accessing them, see my post at Securing Uploaded Files - #2 by KenWhitesell