# Displaying code blocks from a file

**URL:** <https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827>\
**Category:** Getting Started\
**Created:** [January 10, 2024, 10:37pm UTC](https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827 "2024-01-10T22:37:50Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![duboism](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/duboism/32/18305_2.png) [@duboism](https://forum.djangoproject.com/u/duboism)\
**Post date:** [January 10, 2024, 10:37pm UTC](https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827/1 "2024-01-10T22:37:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![addwebsolution](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/addwebsolution/32/23762_2.png) [@addwebsolution](https://forum.djangoproject.com/u/addwebsolution)\
**Post date:** [January 11, 2024, 5:01am UTC](https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827/2 "2024-01-11T05:01:23Z")

</div>

> [@duboism](#):
>
> I would like to write the code in a python script and display it block-by-block alongside the explanations

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?

---

<div class="post-metadata">

**Author:** ![duboism](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/duboism/32/18305_2.png) [@duboism](https://forum.djangoproject.com/u/duboism)\
**Post date:** [January 11, 2024, 8:53am UTC](https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827/3 "2024-01-11T08:53:10Z")

</div>

> [@addwebsolution](#):
>
> 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:

```auto
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"}

```

---

<div class="post-metadata">

**Author:** ![addwebsolution](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/addwebsolution/32/23762_2.png) [@addwebsolution](https://forum.djangoproject.com/u/addwebsolution)\
**Post date:** [January 11, 2024, 9:58am UTC](https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827/4 "2024-01-11T09:58:52Z")

</div>

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.

```auto
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 }}`.

---

<div class="post-metadata">

**Author:** ![duboism](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/duboism/32/18305_2.png) [@duboism](https://forum.djangoproject.com/u/duboism)\
**Post date:** [January 11, 2024, 10:39am UTC](https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827/5 "2024-01-11T10:39:42Z")

</div>

OK, I realize my ignorance, now 🙂 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) ?

---

<div class="post-metadata">

**Author:** ![addwebsolution](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/addwebsolution/32/23762_2.png) [@addwebsolution](https://forum.djangoproject.com/u/addwebsolution)\
**Post date:** [January 11, 2024, 10:41am UTC](https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827/6 "2024-01-11T10:41:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![duboism](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/duboism/32/18305_2.png) [@duboism](https://forum.djangoproject.com/u/duboism)\
**Post date:** [January 11, 2024, 11:03am UTC](https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827/7 "2024-01-11T11:03:44Z")

</div>

OK but how does it relate to my problem ?

---

<div class="post-metadata">

**Author:** ![addwebsolution](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/addwebsolution/32/23762_2.png) [@addwebsolution](https://forum.djangoproject.com/u/addwebsolution)\
**Post date:** [January 11, 2024, 11:59am UTC](https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827/8 "2024-01-11T11:59:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![duboism](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/duboism/32/18305_2.png) [@duboism](https://forum.djangoproject.com/u/duboism)\
**Post date:** [January 11, 2024, 2:03pm UTC](https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827/9 "2024-01-11T14:03:33Z")

</div>

Hum, probably not enough.

---

<div class="post-metadata">

**Author:** ![addwebsolution](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/addwebsolution/32/23762_2.png) [@addwebsolution](https://forum.djangoproject.com/u/addwebsolution)\
**Post date:** [January 12, 2024, 4:51am UTC](https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827/10 "2024-01-12T04:51:35Z")

</div>

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](https://docs.djangoproject.com/en/5.0/intro/tutorial01/) and also take reference from here [Django documentation | Django documentation | Django](https://docs.djangoproject.com/en/5.0/).  
Once get the understanding of Django you will understand what I was trying to say in previous posts.

---

<div class="post-metadata">

**Author:** ![duboism](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/duboism/32/18305_2.png) [@duboism](https://forum.djangoproject.com/u/duboism)\
**Post date:** [January 12, 2024, 2:42pm UTC](https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827/11 "2024-01-12T14:42:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 12, 2024, 2:51pm UTC](https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827/12 "2024-01-12T14:51:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![duboism](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/duboism/32/18305_2.png) [@duboism](https://forum.djangoproject.com/u/duboism)\
**Post date:** [January 12, 2024, 3:39pm UTC](https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827/13 "2024-01-12T15:39:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dim](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/dim/32/18352_2.png) [@dim](https://forum.djangoproject.com/u/dim)\
**Post date:** [January 12, 2024, 4:07pm UTC](https://forum.djangoproject.com/t/displaying-code-blocks-from-a-file/26827/14 "2024-01-12T16:07:55Z")

</div>

_Here, this may help you_  
[django templating language docs](https://docs.djangoproject.com/en/5.0/ref/templates/language/)  
[custom template tags in django](https://docs.djangoproject.com/en/5.0/howto/custom-template-tags/)
