How to add appropriate image size width and height in Django? - django

I have models for image and upload image, how to generate size width and height in django?
article/models.py
class article(models.model):
image1 = models.ImageField(upload_to='images', blank=True)
code head.html
<meta property="og:image" content="{{ article.image1 }}" />
<meta property="og:image:secure_url" content="{{ article.image1 }}" />
<meta property="og:image:width" content=" " />
<meta property="og:image:height" content=" " />```
add size height and width generate

Django ImageField has a built-in feature for this. You just need to add two new columns for width and height:
class article(models.model):
image1 = models.ImageField(width_field='image1_width', height_field='image1_height', upload_to='images', blank=True)
image1_width = models.IntegerField(blank=True, null=True)
image1_height = models.IntegerField(blank=True, null=True)
Then you can simply do:
<meta property="og:image" content="{{ article.image1 }}" />
<meta property="og:image:secure_url" content="{{ article.image1 }}" />
<meta property="og:image:width" content="{{ article.image1_width }}" />
<meta property="og:image:height" content="{{ article.image1_height }}" />```

my models.py
class article(models.model):
image1 = models.ImageField(upload_to='images', width_field='image1_width', height_field='image1_height')
image1_width = models.IntegerField(editable=False, null=True)
image1_height = models.IntegerField(editable=False, null=True)
my code head.html
<meta property="og:image" content="{{ article.image1 }}" />
<meta property="og:image:secure_url" content="{{ article.image1 }}" />
<meta property="og:image:width" content="{{ article.image1_width }}" />
<meta property="og:image:height" content="{{ article.image1_height }}" />

Related

how to pass temporary decrypted data to django template

I have an application that can upload files in every format I want to be encrypted and saved to database I also created decrypt functionality but I dont know how to show this decrypted file to the user or django templates.
i have tried many ways like TemFile library but still I cant pass my decrypted data to the templates
here is my code for encryption
def encrypt (request):
if request.method == 'POST':
upload_file = request.FILES.get("like")
# key generation
key = Fernet.generate_key()
encode_key = urlsafe_base64_encode(force_bytes(key))
print(encode_key)
document = FileEncrypt.objects.create(document = upload_file,key=encode_key)
# using the generated key
fernet = Fernet(key)
# opening the original file to encrypt
with open(document.document.path, 'rb') as file:
original = file.read()
# encrypting the file
encrypted = fernet.encrypt(original)
with open(document.document.path, 'wb') as encrypted_file:
encrypted_file.write(encrypted)
document.save()
return redirect("decrypt_and_show", id=document.id)
else:
return render(request,"index.html",{
})
after redirecting to decrypt page it's not showing the file
here is my decrypt def
def decrypt(request,id):
document = FileEncrypt.objects.get(pk=id)
decode_key =force_text(urlsafe_base64_decode(document.key))
print(decode_key)
fernet = Fernet(decode_key)
# opening the encrypted file
with open(document.document.path, 'rb') as enc_file:
encrypted = enc_file.read()
# decrypting the file
decrypted = fernet.decrypt(encrypted)
tmpfile = tempfile.NamedTemporaryFile(delete=False,suffix='.JPEG')
tmpfile.seek(0)
tmpfile.write(decrypted)
return render(request,"index2.html",{
"d" : tmpfile.name
})
in my template is
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="{% static 'fileuploads/js/script.js' %}" defer></script>
</head>
<body>
<img src="{{d}}" />
</body>
</html>
in my index template is
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="{% static 'fileuploads/js/script.js' %}" defer></script>
</head>
<body>
<form action"" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="like" />
{% comment %} {{form}} {% endcomment %}
<button type="submit">Upload</button>
</form>
</body>
</html>
could you please help me with.

Amazon S3 Bucket css and js folders not accessible to HTML

I've created a simple static website in an S3 bucket using some boilerplate HTML5 code. I've set permissions on the bucket to Public access universally (all files and folders)
I can't work out why my css and js folders are not loading the content in the index.html. The image referenced in the <img/> tag is in the root of the project and renders fine.
I've checked the metadata and it seems to be correct e.g. text/css for the css files.
Here's the layout:
Here's the index.html
<!DOCTYPE html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8" />
<title></title>
<meta name="description" content="" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta property="og:title" content="" />
<meta property="og:type" content="" />
<meta property="og:url" content="" />
<meta property="og:image" content="" />
<link rel="manifest" href="site.webmanifest" />
<link rel="apple-touch-icon" href="icon.png" />
<!-- Place favicon.ico in the root directory -->
<link rel="stylesheet" href="css/normalize.css" />
<link rel="stylesheet" href="css/main.css" />
<meta name="theme-color" content="#fafafa" />
</head>
<body>
<!-- Add your site or application content here -->
<p>My new Website</p>
<img src="tile.png" alt="Girl in a jacket" width="500" height="600" />
<script src="js/vendor/modernizr-3.11.2.min.js"></script>
<script src="js/plugins.js"></script>
<script src="js/main.js"></script>
<!-- Google Analytics: change UA-XXXXX-Y to be your site's ID. -->
<script>
window.ga = function () {
ga.q.push(arguments);
};
ga.q = [];
ga.l = +new Date();
ga("create", "UA-XXXXX-Y", "auto");
ga("set", "anonymizeIp", true);
ga("set", "transport", "beacon");
ga("send", "pageview");
</script>
<script src="https://www.google-analytics.com/analytics.js" async></script>
</body>
</html>
Inside of css folder:
Permissions on css are as follows:
This was a permissions issue on one of the css files.

Open Graphic image not displaying

I have this code which I use in testing the open graphic and this is the code below:
<html>
<head>
<meta charset="UTF-8" />
<title>THE KING</title>
<meta name="robots" content="Just About Me" />
<meta name="description" content="All You Should Know About Me" />
<meta property="og:locale" content="en_US"/>
<meta property="og:type" content="article"/>
<meta property="og:title" content="Just About Me"/>
<meta property="og:description" content="All You Should Know About Me"/>
<meta property="og:url" content="http://share.stephensangkn.org"/>
<meta property="article:section" content="The King"/>
<meta property="article:published_time" content="2016-07-31 15:58:30"/>
<meta property="article:modified_time" content="2016-07-31 15:58:30"/>
<meta property="og:image" content="http://share.stephensangkn.org/images/me.jpg"/>
<script src="js/jquery.js"></script>
</head>
<body>
<h2>THE KING</h2>
<img src="images/me.jpg" style="max-width:500px;max-height:500px" /><br>
<a style="cursor:pointer;">share</a>
<script>
url = "https://facebook.com/sharer.php?u=http://share.stephensangkn.org";
W = "500";
H = "800";
$('a').click(function(){
window.open(url, '', 'width='+W+',height='+H);
});
</script>
</body>
</html>
But the problem is that the image refuse to show up when the Facebook page opens, I even changed the image still same result. Here is the link to my page. Please any help?
I did try your code and I observed what you complained but I shared the link on my Facebook page and it displayed the same photo on your link so try sharing it an see

Auto-play Open Graph Protocol Videos in Facebook Mobile News Feed

Has anyone been able to figure out the open graph tags to get a video to auto-play like Facebook-native uploaded videos do?
I've been able to successfully link a video to my URL in the share preview/url debugger using og:video tags but it doesn't play automatically on mobile like if I upload it directly as a shared video.
These are the open graph tags I have now:
<meta property="fb:app_id" content="_______" />
<meta property="og:type" content="article" />
<meta property="article:publisher" content="https://www.facebook.com/______" />
<meta property="og:site_name" content="_______" />
<meta property="og:url" content="https://_______.com/_______/_______" />
<meta property="og:title" content="_______" />
<meta property="og:description" content="_______" />
<meta property="og:image" content="https://_______.com/images/opengraph.png" />
<meta property="og:video" content="http://_______.com/videos/_______.mp4" />
<meta property="og:video:secure_url" content="https://_______.com/videos/_______.mp4" />
<meta property="og:video:type" content="video/mp4" />
<meta property="og:video:width" content="480" />
<meta property="og:video:height" content="320" />
My videos show up like this when I share them on Facebook:
I'd like them to show up like this (note: this is auto-playing when I scroll through my news feed which happens when you upload a video directly through Facebook):

Facebook share, image not displayed

Just a question about sharing image on social networks as Fb, Twitter... when they are stored on inkfilepicker.
In my head section, I have put opengraph tags or link but image is never displayed when sharing.
<meta property="og:image" content="https://www.filepicker.io/api/file/pZFD49sHQ0yazsb1mTKD">
<link rel="image_src" href="https://www.filepicker.io/api/file/pZFD49sHQ0yazsb1mTKD">
Any ideas where it may come from ?
Thanks,
Jul
<img src=" image link here " />
Please try to write as above .
I'm currently using this Open Graph tags, and they works, i have changed the connection to database in php to the <header> section of the page, without the "og:image:width" and "og:image:height" tags, the first time the article is shared, no image is shown, due to a Facebook cache problem.
<meta property="og:image" content="/path/to/image" />
<meta property="og:image:width" content="image_width_in_pixels" />
<meta property="og:image:height" content="image-height_in_pixels" />
<meta property="og:url" content="url_being_shared" />
<meta property="og:type" content="article" />
<meta property="og:title" content="title_of_article" />
<meta property="og:description" content="description_of_article" />
You can find more information about that in:
https://developers.facebook.com/docs/sharing/best-practices/#precaching