I have a simple Blog model with a TextField.
What is the best way to save links in the TextField ?
1) Saving link in the database in this form: http://www.example.com
and then using some filter in the template to trasform it in form:
<a rel="nofollow" href="http://www.example.com>http://www.example.com</a>
2) Saving link in the database directly in this form:
<a rel="nofollow" href="http://www.example.com>http://www.example.com</a>
Assuming you are talking about how to save links (or any HTML formatting really) that you are including in your blog posts, not standalone links (like for a blogroll):
You might want to think about adopting some sort of markup language like Markdown or BBCode. You’d then store the blog post in the pure markup syntax, and provide a method (like
blog.get_post()) that would parse the stored markup and return HTML. You’d then use thesafefilter in your template to avoid escaping the HTML.Even better, to avoid parsing your markup on every request, cache it in the database. Add a TextField called something like text_html to your
Blogmodel. Override yourBlogmodel’ssavemethod to parse the markup into HTML and save the HTML to the text_html field.Then when you want to render the blog post in a template, you can use
{{ post.text_html|safe }}without having to parse your markup each time.So your Blog model might look like this (only text field shown for clarity)
To make things simpler on the data entry side, you could also add in a nice javascript editor – like the one I’m typing this answer in right now.