Displaying code blocks from a file

Hello,

I’m a beginner with django so pardon my ignorance. I am working on project where we display (python) code snippets to explain some scripts. Currently, the code is hardcoded in the template so it’s rather painful to adapt.

I would like to write the code in a python script and display it block-by-block alongside the explanations if possible including line numbers and syntax highlighting (something like the good old Listings LaTeX package). Is it possible to do something like that ?

I have found some modules based on pygments but it seems that they have not been updated for several years.

Thanks in advance,
Mathieu

By this, do you want that your code with the explanation should be dynamically rendered in your particular template page from D.B. tables instead of writing those in html files?

Exactly. I would like to be able to write something like this:

The next 2 lines show how to load the packages:

{listing file="example_script.py" linerange="5-6"}

Now we cant create the base objects:

{listing file="example_script.py" linerange="8-10"}

Okay, so for this you can create a model where you can define certain fields like title, author, created_at, decription, etc. For the description field, you can use a Html Text field like tinymce which will edit your text as you like.

from django.db import models
from tinymce.models import HTMLField

class Abcd(models.Model):
    ......
    is_active = models.BooleanField(default=True)
    description = HTMLField()

Now when you pass the object or queryset of the Abcd to your page from views, you can use this description as html text and render it within your page like this {{ object.description|safe }}.

OK, I realize my ignorance, now :slight_smile: I will have try to think about all that but could you please what does the Abcd model represents ? The example script or the whole page (explanation + code example) ?

The code I’ve provided was just a simple example of a model and the HTML field that I’ve used in it for the description field.

OK but how does it relate to my problem ?

Don’t mind but do you have knowledge on models ?

Hum, probably not enough.

Okay, no problem.
I would recommend you to first complete the basic knowledge on Django before building a project. You can go through the tutorial from the docs Writing your first Django app, part 1 | Django documentation | Django and also take reference from here Django documentation | Django documentation | Django.
Once get the understanding of Django you will understand what I was trying to say in previous posts.

OK, I was already reading those tutorials (hence my limited knowledge). I will keep you in touch.

You really can’t just “read” those tutorials and expect to fully understand them. You do need to work your way through them - and that includes typing all the code and not copy/paste from the site. That’s the only way to get the most benefit from them. Doing anything else is only short-changing your own education.

Yes, that’s what I meant (although I confess some copy/paste).

Here, this may help you
django templating language docs
custom template tags in django