0

Is there a way to force a field type for a Django model?

I have a model field that's a textarea and when it renders to HTML using the modelform, is outputs the text between:

<textarea>...</textarea>

Is there any way to force django to place the text between two div tags like:

<div>...</div>

The only reason I ask, is because I'd like to use bootstrap-wysiwyg, and there is a limitation to using tags.

https://github.com/mindmup/bootstrap-wysiwyg/issues/54

4

1 回答 1

1

A div is not a field.

You've misunderstood the issue that you link to. You still need a textarea, otherwise you can't submit the content. The wysiwyg library takes care of displaying an editable div, but it looks like it's up to you [*] to take care of populating that div from the textarea, and copying the edited text back to the textarea on submit, both of which you should do in Javascript.

So, here are the steps:

  • Output the field as a normal textarea.
  • In JS, when the page loads, copy the text to a div and instantiate the wsysiwyg library, then hide the textarea.
  • On submit, copy the edited text back to the textarea.

[*] slightly strangely: most rich-text editor JS libraries have code to do that for you.

于 2013-05-01T08:11:44.523 回答