在Django文本字段中不呈现HTML [英] HTML not rendering in Django text field

查看:105
本文介绍了在Django文本字段中不呈现HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用markdown来避免在我的wiki表单中输入HTML,但是由于某种原因,该表单正在显示HTML代码而不是预期的格式。



我的查看功能如下:

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


def view_page(request,page_name):
try:
page = Page。 object.get(pk = page_name)
除了Page.DoesNotExist:
返回render_to_response('create.html',{'page_name':page_name})

content = page。内容
返回render_to_response('view.html',{'page_name':page_name,'content':markdown.markdown(content)})

这是我的view.html模板:

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

{%block title%} {{page_nam e}} {%endblock%}

{%block content%}
< h1> {{page_name}}< / h1>
{{content | wikify}}
< hr />
< a href ='/ mywiki / {{page_name}} / edit /'>编辑此页?< / a>

{%endblock%}

这是我的base.html:

 < html> 
< head>
< title> {{%block title%} {%endblock%}< / title>
< / head>
< body>
< div>
菜单:< a href ='/ mywiki / Start /'>起始页< / a>
< / div>
{%block content%}
{%endblock%}
< / body>
< / html>

我已经安装了降价,我的Django版本是1.4.1(Mac)。 >

谢谢。

解决方案

使用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 }}

这篇关于在Django文本字段中不呈现HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆