且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Django文本字段中不呈现HTML

更新时间:2023-01-11 12:25:22

使用Django的安全过滤器,以便您的Html不被转义。

  {{content | safe}} 


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.

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

{{ content|safe }}