WMD 预览与输出不匹配
我在 Google 应用程序中使用 WMD,网站管理员可以更新网站页面,用户可以看到信息。
预览功能工作正常,我可以按照我想要的方式查看文本,但是当我在用户部分时,返回的 Markdown 没有格式 - 我该如何解决这个问题?
这是我正在使用的代码
{% block content-left %}
{% if is_admin %}
<div id="content-bodyleft" class="wmd-preview"></div>
<form action="/admin/content/" method="post">
<textarea id="markdown" name="markdown" style="width: 400px; height: 200px;" >{{ page_content.html }}</textarea>
<input name="page" type="hidden" value="{{ request.path }}" />
<input type="submit" name="Save" />
</form>
<div class="wmd-output"></div>
<script type="text/javascript">
// to set WMD's options programatically, define a "wmd_options"
// object with whatever settings
// you want to override. Here are the defaults:
wmd_options = {
// format sent to the server. Use "Markdown" to return the markdown source.
output: "Markdown",
// line wrapping length for lists, blockquotes, etc.
lineLength: 40,
// toolbar buttons. Undo and redo get appended automatically.
buttons: "bold italic | link blockquote code image | ol ul heading hr",
// option to automatically add WMD to the first textarea found.
// See apiExample.html for usage.
autostart: true
};
</script>
<div class="wmd-output"></div>
<script type="text/javascript" src="/static/wmd/wmd.js"></script>
{% else %}
{{ page_content.html|markdown }}
{% endif %}
I am using WMD in a google app situation whereby the site administrator can update the pages of the site and the users see the information.
The preview function is working fine and I can see the text the way I want it to appear, but when I am in the users section, the markdown is being returned without the formatting - how can i fix this?
This is the code i am using
{% block content-left %}
{% if is_admin %}
<div id="content-bodyleft" class="wmd-preview"></div>
<form action="/admin/content/" method="post">
<textarea id="markdown" name="markdown" style="width: 400px; height: 200px;" >{{ page_content.html }}</textarea>
<input name="page" type="hidden" value="{{ request.path }}" />
<input type="submit" name="Save" />
</form>
<div class="wmd-output"></div>
<script type="text/javascript">
// to set WMD's options programatically, define a "wmd_options"
// object with whatever settings
// you want to override. Here are the defaults:
wmd_options = {
// format sent to the server. Use "Markdown" to return the markdown source.
output: "Markdown",
// line wrapping length for lists, blockquotes, etc.
lineLength: 40,
// toolbar buttons. Undo and redo get appended automatically.
buttons: "bold italic | link blockquote code image | ol ul heading hr",
// option to automatically add WMD to the first textarea found.
// See apiExample.html for usage.
autostart: true
};
</script>
<div class="wmd-output"></div>
<script type="text/javascript" src="/static/wmd/wmd.js"></script>
{% else %}
{{ page_content.html|markdown }}
{% endif %}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
发生这种情况的原因是 Django 表单只能看到代表 WMD 编辑器的
有多种方法可以在客户端或服务器上解决此问题...
保存表单后,使用 python markdown 模块将 markdown 转换为服务器上的 HTML,像这个
当表单在客户端提交时,有javascript 将 WMD
选项 #1 可能是最简单的。这是一些示例代码...
The reason this is happening is because the Django Form is only seeing the value of the
<textarea>
tag that represents WMD editor. That value is the actual markdown, not the rendered HTML that you see in the preview.There are several ways to fix this, on either the client or the server...
When the form is saved, convert the markdown to HTML on the server using a python markdown module, like this one
When the form is submitted on the client, have javascript replace the value of the WMD
<textarea>
tag to the actual HTMLOption #1 is probably the easiest. Here's some sample code...
这似乎与 WMD.js 没有任何关系,WMD.js 是一个编辑器,与显示内容无关。
您没有发布模型,但看起来您正在将内容输入“markdown”字段,但显示不同的字段“html”。我想你的模型中有一些东西 - 也许在保存时 - 用转换后的标记填充该 html 字段?
另外,您确定看到的是原始 Markdown,还是看到的是原始 HTML?我假设你需要转义 html 输出:
This doesn't appear to have anything to do with WMD.js, which is an editor and has nothing to do with displaying the content.
You don't post your models, but it looks like you are entering content into the "markdown" field, but displaying a different field, "html". I presume you have something in your models - maybe on save - that populates that html field with the converted markup?
Also are you sure you're seeing raw markdown, or are you seeing raw HTML? I would assume you would need to unescape the html output:
这是我的 models.py 文件
,来自views.py
为了帮助您了解正在发生的情况,例如,我正在键入
并在预览中看到
标题
副标题
文本文本文本
,但在输出时我看到
谢谢,我非常感谢你在这方面的帮助。
This is my models.py file
and this is from views.py
To help you understand what is happening, for example I am typing
and seeing
Title
Subtitle
Text text text
in preview, but on output i am seeing
Thanks, I do appreciate your help with this.