1

Question from Project 1 in CS50W

I'm stuck trying to get the markdown info from files to my HTML. I manage to get the markdown info into a HTML string, but when I input it to my HTML I get the same string, it won't take them as different elements. Something like this:

Exact string in site

The code I'm using in views.py is the following:

def entry(request, title):

info = util.get_entry(title)

if info == None: 
    info = "File Not Found"

else:
    info = markdown.markdown(info)
    print(info)

return render(request, "encyclopedia/entry.html", {
    "title": title,
    "info": info
})

And the code in my HTML:

{% extends "encyclopedia/layout.html" %}

{% block title %}
    {{ title }}
{% endblock %}

{% block body %}

    {{ info }}

{% endblock %}

Thanks in advance for the support!

2 Answers 2

1

A possible method has been mentioned in the Hints part.

By default, when substituting a value in a Django template, Django HTML-escapes the value to avoid outputting unintended HTML. If you want to allow for an HTML string to be outputted, you can do so with the safe filter (as by adding |safe after the variable name you’re substituting).

safe filter : https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#safe

1
  • Thanks! Way easier than what I found. Dec 1, 2020 at 19:16
0

Finally got the answer a few minutes after posting the question! In this post the collaborator tells that you have to "set the autoescape off", whatever that means :), but it works perfectly! This is how the HTML changed:

From:

{% extends "encyclopedia/layout.html" %}

{% block title %}
    {{ title }}
{% endblock %}

{% block body %}

    {{ info }}

{% endblock %}

To:

{% extends "encyclopedia/layout.html" %}

{% block title %}
    {{ title }}
{% endblock %}

{% block body %}

    {% autoescape off %}{{ info }}{% endautoescape %}

{% endblock %}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .