I have a text field in my MySQL table. I want to format all new lines with the <br> or some sort of formatting for the template. Is there anything built into the framework for this? I tried to read into the following page:
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs
But seems like that page won't work for this? Is there another documentation I can refer to? Thanks!
It sounds like you need the linebreaksbr template filter.
Normally, you would use it in the template:
{{ instance.fieldname|linebreaksbr }}
However, it's possible to import it and use it in your view as well:
from django.template.defaultfilters import linebreaksbr
text_with_br = linebreaksbr(instance.fieldname)
The advantage of using linebreaksbr instead of writing your own snippet, is that the linebreaksbr takes care of autoescaping for you.
I decided to do it the following way: "<br />".join(word.split("\n")). Not sure if that's the best way...still digging into it. It certainly works though!
It may be overkill for you depending on your use case, but I use django-tinymce in my admin area to add rich text editing fields to charfields that will be used in templates. This saves a html string in the database and in your template you can just use:
{{ model.field|safe }}
to output it without losing the html formatting. It's quite easy to set up.
Related
I've been stuck on this for an embarrassingly long time. I have two inputs that aren't displaying correctly, a markdown widget and the list widget. They both appear as one long string. I thought I needed to add a markdown parser for the former at least so I'm using markdown-it in a manner similar to this:
https://github.com/11ty/eleventy/issues/236
It is adding paragraph breaks where they should be but they show up on the page as p tags. I thought this was because I already had the parsed text nested between p tags but if I delete those nothing shows up at all. When I look at the html file created by eleventy, the tags show up as "< ;p> ;" (without the spaces) which it seems the browser isn't reading correctly when trying to interpret the html. I'm using nunjucks for templating if that matters. My .eleventy.js file looks like this currently. What am I missing? Also the markdown filter seems to only want to take a string so I'm not sure where to even begin with the list.
By default, Nunjucks HTML-escapes all variables when outputting templates. This is what you want most of the time, unless you're trying to render HTML input.
You might want to try using the safe filter after your markdownify filter.
{{ markdownContent | markdownify | safe }}
I have just started with Pelican. It's awesome, I just can't figure out how to use macros in my articles (and pages). I know I can use Jinja when making my own theme, but I can't seem to be able to use it in articles. I'd like to be able to define a macro/function/template/whatever that I'd put directly in the markdown of the article, possibly with parameters, and it would get expanded when the pages are generated. For example a function to generate an image with a caption of given size and css class that would also be a link directly to the image. I'd like to be able to access these macros from all articles, to be able to reuse them everywhere. Something I'd normally do with PhP.
I could probably use JS to do this, but if I'd like to avoid it and keep everything static if possible. Can this be done?
UPDATE:
I found a pelican plugin that solves this - jinja2content.
OLD SOLUTION:
I found a solution here. You can implement a filter in Python to process all texts in articles/pages like this:
Create a python file filters.py in which you write the filter function process_text to expand my macros (or generally do anything with the article/page text), for example to test the function write something like:
def process_text(input_text):
return "TEST " + input_text
In the Pelican config file (pelicanconfig.py) register this function as a possible filter to be used with Jinja:
import sys
sys.path.append('.')
import filters
JINJA_FILTERS = {'process_text':filters.process_text}
Now you have to edit the templates to apply this filter to article/page texts before adding them to output. In my case I edited two files: themes/themename/templates/article.html and themes/themename/templates/post.html and changed {{ article.content }} to {{ article.content|process_text }} and {{ page.content }} to {{ page.content|process_text }} in them to apply the filter.
Now all texts in articles and pages should be prefixed with "TEST".
The not so convenient thing about this is I have to write my own macro expander, which shouldn't be extremely hard with regular expression in Python, but if there is a nicer way to do this, feel free to post here.
I have set of code of tutorial INVO as pre the Phanlcon Official documentation.
I just want to customize the code as per my need, So I wrote some code to prepare a fresh Module.
I created the below files that are needed to create the full module
app/controllers/AbcController.php
app/model/Abc.php
app/forms/AbcForm.php
app/view/index.volt
app/view/edit.volt
app/view/search.volt
app/view/new.volt
After creating all these file, I am able to runt he module,but In this particular module I am not able to see the tabs having the links.
I think I am missing something, Help me out.
I face the same problem recently, and even the solution is not there in the official documentation of Phalcon.
I came with the solution take reference from another module. There is one file missing in your Code structure that is layout file.
For any module you need to define a layout at least. You have to create a file as below.
app/view/layouts/abc.volt
In this you have to put the code according to your need that where you want to show the content where center or left align.
{{ elements.getTabs() }}
<div align="center">
{{ content() }}
</div>
Here {{ elements.getTabs() }} is used to show the tab. Hope you will get the solution.
I also came with the updated answer.
There could be different layout for any view. You just need to define the layout same as the class name. That's It. !
app/view/layouts/test.volt
or
app/view/layouts/test.html
i was just wondering (since i didn't find anything quick on Google) if its possible (and how do i achieve that) to search directly in an html file, and ignore the tags or not as i please?
explaining a bit further. we wrote a crawler and obviously the crawler gives back the HTML of the page. But if i feel like searching the content of the crawler, do i need 2 separate fields one with html and one without or i can just have one field with html and search ignoring the html tags or not.
thanks in advance.
If i correctly understand you, all you need is to set search indexes without html tags?
We solved that problem this way:
class PostIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(model_attr='text', use_template=True, document=True)
and in template (search/indexes/blogs/post_test.html) we just used striptags filter
{{ object.content|striptags }}
After that you need to build_schema and rebuild_index. Now it search correctly without tags.
I've added the Dojo wysiwyg editor to my django admin panel. I have an issue when I type more than one space. When I output the syntax created by the editor to a template I get for each extra space. I assume it's trying to create a non-breaking space, but it renders in the browser as .
Does anyone know why this is? How would I go about fixing this?
I think its django who is changing & to & on serverside. If its was a simple space django would have replaced it with by itself. I donno if there is any feature to turn off escaping for is specific case in django you can try that
After a little research you want to use the template filter safe to fix this issue. You'll probably also want to add the filter removetags with script as an option to remove potentially malicious javascript. So my template variable ends up looking like this: {{ var|removetags:"script"|safe }}