我如何在我的 django 表单上连接 WMD 编辑器?
我如何将 WMD 编辑器连接到我的 django 表单?
How can i hook-up WMD editor on to my django forms?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我如何将 WMD 编辑器连接到我的 django 表单?
How can i hook-up WMD editor on to my django forms?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
这是完整的 Django 小部件类:
在表单定义中使用它,例如
text = forms.CharField(widget=WMDEditor)
。Here is complete Django widget class:
Use it in your form definition like
text = forms.CharField(widget=WMDEditor)
.从当前 WMD 下载的 readme.txt 中:
因此,将必要的代码添加到用于呈现表单的模板中,并确保要使用 WMD 的文本区域是页面上的第一个,然后就可以开始了。
From the readme.txt in the current WMD download:
So, add the necessary code to the template you're using to render the form, and make sure that the textarea you want to use WMD on is the first on the page, and you'll be good to go.
我刚刚做完这件事。 还有一些需要注意的微妙之处,其他答案中没有涵盖(到目前为止)。
首先,为了让 Django 在提交表单时正确检索 WMD 编辑器的值,需要设置 Django 的 id="" 值。 通常这看起来像'id_'
但是 #1 产生了一个问题:WMD 编辑器经过硬编码来查找 id="wmd-input" 以了解它使用的文本区域。
因此,我们需要的是一种将 id 属性的值传递给 WMD 的方法。 我自己通过让 Django 模板呈现一个全局 javascript 变量来完成此操作,当执行时,WMD 将使用客户端来正确定位 textarea 标记。
如果我们重新制作 WMD id 标签,那么我们还需要确保 CSS 仍然有效
最后,为了让 Django 预先填充该值,我们需要确保该值在 textarea 标记中呈现
因此这里有一些代码。
widgets.py
wmd/widget.html
(将其命名为您的应用程序所需的任何名称)
注意:您可能需要根据您的处理方式(自定义模板标记、中间件等)调整 MEDIA_URL。 如果您是 Django 新手并且不明白我刚才所说的内容,只需暂时硬编码该值即可使其正常工作,并稍后了解这一切意味着什么。
最后,您需要将 1 个小项设置为编辑 WMD 源(请注意,我使用的是 StackOverflow 分支,因此对于其他版本来说这可能略有不同)
wmd.js
如果您正在使用 wmd.css 并且尚未编写自己的文件,那么您将还需要对此进行一些小小的更新。 因为该元素不再是 #wmd-input,所以我们需要更新它以确保它使用 wmd-input 类:
wmd.css
唷! 那是一群。 希望对大家有帮助。
I just finished doing this. There are a couple more subtleties to be aware of that aren't covered in the other answers (so far).
First, in order for Django to properly retrieve the value of WMD editor when the form is submitted, it needs to have Django's value for id="" set on it. Normally this would look something like 'id_'
But #1 creates a problem: WMD Editor is hard-coded to look for id="wmd-input" to know what text area it makes use of.
Therefore, what we need is a way to pass the value of the id attribute to WMD. I've done this myself by having the Django template render a global javascript variable, that when executed client side will be used by WMD to properly locate the textarea tag.
If we're re-doing the WMD id tag, then we'll also need to make sure the CSS still works
Lastly, in order for the value to be pre-populated by Django, we need to make sure that value is being rendered in the textarea tag
So here's some code for you.
widgets.py
wmd/widget.html
(name this whatever you need for your app)
NOTE: You might need to adjust the MEDIA_URL depending on how you're handling that (custom template tag, middleware, whatever). If you're new to Django and don't understand what I just said, just hardcode the value in for now to get it working and learn what that all means later.
Lastly, you'll need to make 1 minor edit to the WMD source (note that I'm using the StackOverflow fork, so this might be slightly different for other versions)
wmd.js
If you're using the wmd.css and haven't written your own already, you'll also need to make a slight update to that. Because that element is no longer #wmd-input, we'll need to update it to make sure it uses the wmd-input class:
wmd.css
Whew! That was a bunch. Hope that helps everyone.