I have a bunch of posts stored in text files formatted in yaml/textile (from Jekyll) that I am trying to import into my new Django project. The problem is that Django is escaping the actual html code, meaning my post is not getting formatted. How can I got about fixing this? should I change something in the jekyll-import command (a custom manage.py command), the postgresql server, or the views.py file?
Example:
The one thing I can’t do is write about myself. Hell, look at my about me page.
Well, I figured it out right after I posted it. I had tried this before but used the wrong syntax. To do this I just had to add '|safe' to the end of my body tag.
Like so:
{{ body|safe }}
Very nice.
Related
I am trying to have VS Code format my Django HTML files but I am getting:
There is no document formatter for 'django-html'-files installed.
The solution I found on the web works with Beautify, not Prettier.
How can I make it work with Prettier?
#Tedkovsky's answer might technically address the error you're getting, but once you're past that, you'll see that Prettier will mangle your templates since it tries to break long lines containing template tags like {{ }} and {% %}.
This is because Prettier currently (as of 2021-01-09) doesn't support Jinja or Django templates, and for the time being, it looks like the developers aren't interested in adding this functionality. There are 2 (closed) tickets about it here:
https://github.com/prettier/prettier/issues/5581
https://github.com/prettier/prettier/issues/5754
I wasn't able to find a plugin for it either, so it doesn't look like there's a solution for using Prettier with Django templates.
edit: I've been following this thread in the Django forum about autoformatters for Django templates. Perhaps something might materialize there.
later edit: Looks like djhtml can handle indentation, although it's separate from Prettier. It doesn't do full autoformatting, though.
even later edit: djlint can also be used for formatting templates
In settings.json, try
"[django-html]": {
"editor.defaultFormatter": "prettier"
},
According to Ruby on Rails Guides (http://guides.rubyonrails.org/i18n.html#using-safe-html-translations) all I need to do to render my translations without calling html_safe on them is to have the key name end with _html. This is what I tried:
en:
breadcrumbs:
root_html: "<i class='material-icons'>home</i>"
Calling it like this:
I18n.t('breadcrumbs.root_html')
causes the output to be this very string defined inside my translations, but not the rendered HTML.
What am I doing wrong?
Using Ruby on Rails 4.2.1.
Thanks in advance!
Scrolling down a little further in the guide I found the problem:
Automatic conversion to HTML safe translate text is only available from the translate view helper method.
Since I tried to prepare the link inside my controller and pass it through a gem to the view, this didn't work.
To make this work you'll have to call html_safe on the string, like so:
I18n.t('breadcrumbs.root_html').html_safe
If you find another solution, hit me up!
Thanks for everyone in advance.
I encountered a problem when using Scrapy on Python 2.7.
The webpage I tried to crawl is a discussion board for Chinese stock market.
When I tried to get the first number "42177" just under the banner of this page (the number you see on that webpage may not be the number you see in the picture shown here, because it represents the number of times this article has been read and is updated realtime...), I always get an empty content. I am aware that this might be the dynamic content issue, but yet don't have a clue how to crawl it properly.
The code I used is:
item["read"] = info.xpath("div[#id='zwmbti']/div[#id='zwmbtilr']/span[#class='tc1']/text()").extract()
I think the xpath is set correctly and I have checked the return value of this response and it indeed told me that there is nothing under this directory. Results shown here:'read': [u'<div id="zwmbtilr"></div>']
If it has something, there should be something between <div id="zwmbtilr"> and </div>.
Really appreciated if you guys share any thoughts on this!
I just opened your link in Firefox with NoScript enabled. There nothing inside the <div #id='zwmbtilr'></div>. If I enable the javascripts, I can see the content you want. So, as you already new, it is a dynamic content issue.
Your first option is try to identify the request generated by javascript. If you can do that, you can send the same request from scrapy. If you can't do it, the next option is usually to use some package with javascript/browser emulation or someting like that. Something like ScrapyJS or Scrapy + Selenium.
Currently I have a working Django 1.9 application using Python 3.5 in development. The database is Postgres 9.4.2.0.
I have a TEXT type field in the database which contains raw input gathered from users, which is then rendered back for other users to read.
The raw text contains newlines and whatnot which look like:
chat.freenode.net\r\n#randomchannel
The HTML template itself attempts to replace the line breaks with break tags and escape anything else
{{ post.body|linebreaksbr|escape }}
But it doesn't seem to matter what filters I add to the post.body, it always renders the raw \r\n and never replaces the values with <br> tags.
I am not getting any errors in the development server and the rendering of the template works fine, it just seems the filters are not working.
I'm pulling my hair our trying to work out why these filters are not working. Does anyone have any ideas?
Turns out this had nothing to do with Django itself, which is not surprising.
The data migration which happened between the last and current version broke the newlines in the raw data. Therefore the linebreaksbr was working, but didn't find any linebreaks.
I'm using markdown to format some comments in a Django app.
If I try to combine markdown and urlize, inevitably bad formatting errors happen (links get added where they don't belong or aren't recognized, and of course the errors change depending on which filter I use first).
Basically I'd like a filter that does markdown and automatically turns links into hyperlinks if not done so by markdown.
Otherwise, I suppose I'll have to roll my own filter, which I would so rather not do.
What I do is use the Markdown urlize extension.
Once installed, you can use it in a Django template like this:
{{ value|markdown:"urlize" }}
Or in Python code like this:
import markdown
md = markdown.Markdown(safe_mode=True, extensions=['urlize'])
converted_text = md.convert(text)
Here is the start of the Markdown extension docs in case you need more info.