django handle newlines in textarea - django

<textarea id="my" ></textarea>
p=post(body=value)
p.save()
Here I am trying to save the content of the textarea to database but i see that new lines are converted to spaces and then saved.How to handle this on the server side..

They're almost certainly not being converted to new-lines, the problem is likely to be the way you're displaying them.
Try displaying them in your template using the linebreaks filter, like this:
{{ mypost.body|linebreaks }}

Related

How to preseve newline in django posts without using a WYSIWYG editor?

I am using django's forms class along with it's cleaned_data method to get user posts/comments. The problem is that the newlines are not preserved in the cleaned_data field so
The firs line of comment.
And the second lines.
Ends up being
The firs line of comment. And the second lines.
Which is not good.
How to avoid this without using a WYSIWYG editor?
If you're referring to the output of the value held in the field, you just need to use the linebreaks or linebreaksbr filter: https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#linebreaks
{{ instance.field|linebreaks }}
or
{{ instance.field|linebreaksbr }}
to get the correct formatting without having to use a wysiwyg editor.

Using Bootstrap wysiwyg text editor in Django Form

I am using Django and Bootrap 2.32. I want to include this wysiwyg-bootrap-themed text editor: http://mindmup.github.io/bootstrap-wysiwyg/. The usage of this editor is fairly simple, including
$('#editor').wysiwyg();
in the JS-declaration will render each
<div class=editor></div>
into a beatiful wysiwyg text-editor.
Now the problem: I want to include this editor into one of my django form field. I have the single form:
class Article_Form(ModelForm):
Article_text = CharField(widget=Textarea(attrs = {'id' : 'editor'}))
class Meta:
model= Article
, whereas the Article model includes one simple CharField . Is there any chance, to get the editor work inside the Article_text form-field? With the above-mentioned widget, the created textarea cannot be controlled by the wysiwyg-editor-control buttons. Wrapping the form-template-tag like this
<div id="editor">
{{ Article_Form.Article_text }}
</div>
doesn't work either. The problem thus is that Django creates a textarea, wheras the editor would need a <div> to render correctly. Do you guys have any idea how to get this to work (without refering to django-wysiwyg).
Thanks!
I don't know enough about Django but I wrote the editor you're referring to, so here's a suggestion. Assuming the other answer on this page is correct and you can't generate a div directly, you can generate a text area using whatever Django templates you would normally do, then assign two events:
1) page onload event that would copy the textarea contents into the div, something like
$('#editor').html($('#textarea').val())
2) form onsubmit event that would reverse copy the current div contents into the textarea before it gets submitted
$('#textarea').val($('#editor').html())
Take a look at this.
Summernote is a simple WYSIWYG editor based on Twitter's Bootstrap.
django-summernote plugin allows you to embed Summernote into your Django admin page very handy.
https://github.com/lqez/django-summernote
Are you sure that this "plugin" doesn't work with textarea?
{{ Article_Form.Article_text }}
will be rendered to something like:
<textarea cols="40" id="id_Article_text" name="Article_text" rows="10"></textarea>
So there is a chance that you can initialize the wysiwyg editor like:
$('#id_Article_text').wysiwyg();
However after checking the plugin, I doubt that would be possible since it is using contenteditable="true" attribute of HTML5 and probably the plugin works with div only.
So there is no way you can make it work natively with Django form. The solution should be display other fields of your form manually, hide the one with textarea and display the editor instead:
<form action="" method="POST">
{{ Article_Form.field1 }}
{{ Article_Form.field2 }}
<div class=editor></div>
{% csrf_token %}
<input type="submit" value="Submit" id="submit-btn" />
</form>
Then you can use JS to submit your form:
$('#submit-btn').click(function (e) {
e.preventDefault();
$.ajax({
// do your magic here.
// note that you can get the content of the editor with: $('#editor').cleanHtml();
})
});
This way is hackish I agree so I don't recommend you go for it, just find other plugin then. Also please read PEP 8 carefully.
Hope it helps.
Take a look at this repo: https://github.com/rochapps/django-secure-input
I think it solves most of your problems.

Django: show raw html (from database) as html, not rendered

I have created a rich text area using tinymce. I have now saved this to my database and want to display it. It gets saved in the database as html with tags, etc. To display it I have tried:
In views:
content = get_content_from_db()
render_to_response("template.html", {"content":content})
In template:
{{content}}
This shows the content with the tags like so:
<strong>My text</strong>
instead of just displaying the text as bold.
I have also tried using the verbatim tag, but this (doh!) displays as:
{{content}}
How should I do this? Do I need to save it differently? I save it the way I save any other TextArea except the field is a blob.
Use safe filter
{{ content|safe }}

Problem with displaying content made with WYSIWYG in django admin

In one of my projects there was need to implement WYSIWYG-editor into django admin. I've installed http://code.google.com/p/django-tinymce/. Everything works well, but there is a problem with rendering the content made with WYSIWYG-editor. As a result, on html page returns special chars instead of normal html-tags and I see "plain" html tags with no html-layout.
Maybe the problem is in the templates? I simply output variable like {{ content }}
try {{ content|safe }}
Marks a string as not requiring
further HTML escaping prior to output.
via safe

Django textarea widget <p>

In django admin I am using the textarea widget.
When I save data, it is saved inside a <p></p> tag.
I dont want this - any solutions, I just want to get the data out without being wrapped in a <p>.
Any suggestions?
I don't know how to save it without html coding, but maybe it is enough to you to display without that . Just put a |safe in the template after the variable name, example:
{{ variable|safe }}
http://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs