Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML not rendering in Django text field

I am attempting to use markdown to avoid having to type HTML within my wiki form, but for some reason the form is displaying HTML code instead of the intended formatting.

My view function is as follows:

from django.shortcuts import render_to_response
from mywiki.wiki.models import Page
from django.http import HttpResponseRedirect
import markdown


def view_page(request, page_name):
    try:
        page = Page.objects.get(pk=page_name)
    except Page.DoesNotExist:
        return render_to_response('create.html', {'page_name':page_name})

    content = page.content    
    return render_to_response('view.html', {'page_name':page_name, 'content':markdown.markdown(content)})

This is my view.html template:

{% extends 'base.html' %}
{% load wikilink %}

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

{% block content %}
        <h1>{{page_name}}</h1>
        {{content|wikify}}
        <hr/>
        <a href='/mywiki/{{page_name}}/edit/'>Edit this page?</a>

{% endblock %}

And this is my base.html:

<html>
    <head>
        <title>{{% block title %}{% endblock %}</title>
    </head>
    <body>
<div>
Menu: <a href='/mywiki/Start/'>Start Page</a>
</div>
        {% block content %}
        {% endblock %}
    </body>
</html>

I do have markdown installed, and my Django version is 1.4.1 (Mac).

Thanks.

like image 358
entrepaul Avatar asked Sep 13 '12 23:09

entrepaul


2 Answers

Use Django's safe filter so as for your Html not to be escaped.

{{ content|safe }}
like image 132
thikonom Avatar answered Nov 02 '22 23:11

thikonom


{% autoescape off %}
{{content|wikify}}
{% endautoescape %}

maybe ...

like image 22
Joran Beasley Avatar answered Nov 02 '22 23:11

Joran Beasley