I am making a project where I want to display a user's code. I am using Django's forms. But when I post that form, the form doesn't preserve the white space and the line breaks.
Can you please help me?
# this is my form
class VigenereCipherDiscussForm(forms.ModelForm):
class Meta:
model = VigenereCipherDiscuss
fields = ['text', 'share']
widgets = {
"text": forms.Textarea(attrs={"class":"form-control", "rows":4}),
"share": forms.Textarea(attrs={"class":"form-control", "rows":5, "placeholder": "Put in your cipher text or code in here to share with others"})
}
# (stackoverflow preserves the line breaks, the "class Meta" is indented nicely)
If i have this code:
x = 2
if x == 2:
return "2"
else:
return None
# I want this code to be outputted in the form the way it is right now!
But django gives me
x=2 if x==2: return "2" else return None
Use linebreak
change your code to:
x=2\nif x==2:\nreturn "2"\nelse:\nreturn None
and in template:
{{ value|linebreaks }}
you can use tinymce to create a user's code.it will save it in html.
and in your .html add this:{{ value|linebreaks|safe }}
the tinymce Like this:
Related
I am replacing an old program for template-merging with docxtemplater and am trying to recreate the old programs prefix functionality.
I want the line removed if all prefixed tags ({$tag}) on that line are undefined.
The issue being that if all the tags on that line are undefined docxtemplater still creates a blank line.
All the examples I have found online tend to reference inverted-sections or rawtags, which both seem to be designed for a single tag per line opposed to multiple tags side by side.
I have looked into using rawtags and writing a custom-parser / nullGetter. However I am still none the wiser to removing the blank line.
I am using:
const options = {
paragraphLoop: true,
linebreaks: false,
parser: function(tag) {
return {
get(scope, context) {
console.log(tag);
console.log(scope);
console.log(context);
if (tag[0] == "$") {
tag = tag.substr(1); // needs to then remove line break
}
return scope[tag];
}
}
},
nullGetter: function nullGetter(part, scopeManager) {
if (!part.module) {
return "";
}
if (part.module === "rawxml") {
return "";
}
return "";
}
};
doc = new Docxtemplater(zip, options);
The prefix in the program I am replacing acts as follows:
data:
existingtag: EXISTINGTAG
Template.docx:
1 text above
{$existingtag}{$nonexistingtag}
text below
2 text above
{$existingtag}{$existingtag}
text below
3 text above
{$nonexistingtag}{$nonexistingtag}
text below
old program produced (What I want to produce)
1 text above
EXISTINGTAG
text below
2 text above
EXISTINGTAGEXISTINGTAG
text below
3 text above
text below
my docxtemplater produces (extra line in example 3):
1 text above
EXISTINGTAG
text below
2 text above
EXISTINGTAGEXISTINGTAG
text below
3 text above
text below
I'm the creator of docxtemplater and I don't think that there is a way to do what you want to achieve without taking a lot of time to handle this case.
The problem is that the tags such as :
{xxx}{yyy}
have access only to the text that they are in, but they cannot have any effect ouside of that text, so it is not possible to remove a paragraph conditionnally.
There is one tag that has access to the whole paragraph, that is the raw xml tag, prefixed by a "#", like this :
{#raw}
It is used to add rawXML and if the raw value is an empty string, than that paragraph will be removed.
Edit : I have actually worked on a module back in the time to achieve quite similar functionnality, it is a paid module : https://docxtemplater.com/modules/paragraph-placeholder/
I have a django project
I have an models with a booleanField -> checkbox in the form
But I don't know why the checkbox is sticked to the label.
I try to select the html element to add specific css but the 2 elements move together...
current result: [ ]I have read... (the 2 elements are sticked)
expected result: [ ]......I have read...
models.py
class Aveugle(models.Model):
unb_ide = models.AutoField(primary_key=True)
unb_val = models.BooleanField("I have read ITBM procedure and want to unblind treatment", null=True, blank=True)
forms.py
unb_val = forms.BooleanField(label = _("I have read ITBM procedure and want to unblind treatment"))
Try:
This is a temporary solution, but it's just a patch, you need to handle css and html better to make it more solid.
Using Telegram Bot API,
I'm aware that it is possible to send an image via https://core.telegram.org/bots/api#sendphoto
However, how can I embed a remote image into a formatted message?
The message I am looking to send, can be compared to a news article with a title in bold, an image, and a longer text with links. I figured out how to create bold text and links with markdown, but I'm failing at inserting images. How can we do that?
you must set ParseMode in HTML and set your Image Url in A tag like this:
-> never show in message
You can use zero-width space trick. Works for both Markdown and HTML parse mode.
Markdown:
$data = [
'chat_id' => $chat_id,
'parse_mode' => 'markdown',
'text' => "[](https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/Stack_Overflow_logo.svg/200px-Stack_Overflow_logo.svg.png) Some text here.",
];
Result:
Note: The zero-width space is in-between the brackets "[]".
import requests
text="testing"
img="http://imageurl.png"
r = requests.get('https://api.telegram.org/botyour_token_here/sendMessage?chat_id=#your_channel_here&parse_mode=markdown&text='+"[]("+img+")"+text)
Method using <a href=http://.......jpg>..</a> will show preview of the image below the text.
Like this:
a href sample
It will look better if you send an image with a caption.
caption sample
You should just add captions
bot.send_video(user_id, video, caption='some interesting text')
In our case captions are text. look this image
Using sendPhoto rather than sendMessage is a cleaner way of achieving this, depending on your use case, for example:
import io
import json
import requests
telegram_bot_token = 'INSERT_TOKEN_HERE'
chat_id = '#INSERT_CHAT_ID_HERE'
bot_url = 'https://api.telegram.org/bot' + telegram_bot_token + '/sendPhoto'
img_url = 'https://upload.wikimedia.org/wikipedia/commons/thumb/0/02/Stack_Overflow_logo.svg/200px-Stack_Overflow_logo.svg.png'
msg_txt = '<b>Stack Overflow Logo</b>'
msg_txt += '\n\nStack Overflow solves all our problems'
payload = {
'chat_id': chat_id,
'caption': msg_txt,
'parse_mode': 'html'
}
remote_image = requests.get(img_url)
photo = io.BytesIO(remote_image.content)
photo.name = 'img.png'
files = {'photo': photo}
req = requests.post(url=bot_url, data=payload, files=files)
response = req.json()
print(response)
What I need is an Angular or CSS solution which will add a "read more" button if the text is more than 5 lines long.
I have an Angular page which displays text, which has a letter limit of 150 characters.
{{post.post_text | limitTo:letterLimit}}
But in some instances the posts are too long, because they have been written with many many line breaks ie :
my post
line 2
line 3
l
i
n
e
...still less than 150 chars, but it begins to break my page.
So I need is an Angular or CSS solution which will add a "read more" button if the text is more than 5 lines long.
Forgive me, but this is my first attempt at Angular, I don't know where to start! Any help would be much appreciated.
so far
I have only found answers and tutorials which relate to number of characters. I really need a solution based on number of lines or total line-height. Thanks.
You can try such a filter:
{{post.post_text | limitTo:letterLimit | maxLines: linesLimit}} // toggle lines limit with the show more
app.filter('maxLines', function() {
return function(txt, limit) {
var parts = txt.split("\n");
if(limit == 0) return parts.join('<br/>');
return parts.slice(0,limit).join('<br/>');
}
})
In view:
<button class="show-more" ng-if="checkLines(checkLines(resource.description)"></button>
In Controller:
$scope.lines_limit_default = 3;// ex.
function checkLines(txt) {
if(txt && txt != null) {
return (txt.split("\n").length > $scope.lines_limit_default) ? true : false;
} else return false;
};
I have a horizontal SerialChart with AmCharts:
When displayed at a desktop browser, everything looks fine:
But when i'm resizing, the bars are getting smaller, but the text
stays the same:
Is there a way of resizing the categoryAxis as well so i see the bars
in a mobile screen? Or move the Labels on top of the bars when i'm
viewing them on mobile?
Try this, it will help to you.
"categoryAxis": {
"ignoreAxisWidth": true,
"autoWrap": true
}
If you look at this example I have just done, http://jsfiddle.net/17xraeue/, you will see that I have added a line break in standard HTML tags, BR, into the name.
"dataProvider": [
{
"country": "All Other<br>Countries",
"visits": 441,
"color": "#CD0D74"
}
]
To make this work dymanically without having to add line breaks in yourself you could also use a scripting langauge such as PHP to add them for you.
<?
//See http://php.net/manual/en/function.wordwrap.php for more details
//wordwrap([text string], [number of characters before line break], [HTML to use for line break]);
echo wordwrap("this is my text string", 20, "<br />");
?>