我在网上有一个django博客,我终于把facebook分享插件放在了一个。但每次我分享,我都可以分享相同的图像,图像是base.html头部的静态图像。我正在使用django 1.10,python3.6并没有一个共享插件。 如何从各自的页面分享相应的图像?很感谢! ps:我认为其他任何代码都是不必要的。
views.py
def index(request):
posts = Evento.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
return render(request, 'core/index.html', {'posts': posts})
from urllib.parse import quote_plus
def post_detail(request, pk):
post = get_object_or_404(Evento, pk=pk)
share_string = quote_plus(post.apresentacao)
context = { "title": post.nome, "instance": post.foto, "share_string": share_string, }
Evento.objects.get(pk=pk)
return render(request, 'core/post_detail.html', {'post': post})
def index(request):
posts = Evento.objects.filter(published_date__lte=timezone.now()).order_by('-published_date')
return render(request, 'core/index.html', {'posts': posts})
def post_detail(request, pk):
post = get_object_or_404(Evento, pk=pk)
share_string = quote_plus(post.apresentacao)
context = { "title": post.nome, "instance": post.foto, "share_string": share_string, }
Evento.objects.get(pk=pk)
return render(request, 'core/post_detail.html', {'post': post})
models.py
class Evento(models.Model):
nome = models.CharField(max_length=200, null=False, blank=False)
apresentacao = models.TextField(null=False, blank=False)
foto = CloudinaryField('foto', null=True, blank=True)
created_date = models.DateTimeField(
default=timezone.now)
published_date = models.DateTimeField(
blank=True, null=True)
def publish(self):
self.published_date = timezone.now()
self.save()
def __str__(self):
return self.nome
def get_absolute_url(self):
#return reverse("detalhe", kwargs={"pk": self.pk})
return "/post/%s" %(self.pk)
和post_detail.html
<div class="post">
<div class="fb-share-button" data-href="{{ request.build_absolute_uri }}" data-layout="button_count" data-size="large" data-mobile-iframe="true"><a class="fb-xfbml-parse-ignore" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2Fplugins%2F&src=sdkpreparse">Compartilhar</a></div>
{% if post.published_date %}
<div class="date">
{{ post.published_date }}{{ post.timestamp }}
</div>
{% endif %}
<p><h1>{{ post.nome }}</h1></p>
<center><img>{% cloudinary post.foto %}</img></center>
<p><h2>{{ post.apresentacao|linebreaksbr }}</h2></p>
</div>
<div class="fb-comments" data-href="{{ request.build_absolute_uri }}" data-numposts="2"></div>
{% endblock %}
再见。
答案 0 :(得分:0)
选择共享图片的recommended way是将相关的元opengraph og:image
标记添加到您的html头部。看起来应该是这样的。使用django模板代码在每个元标记中插入正确的content
。
<html>
<head>
<title>My awesome blog | Why kittens are great!</title>
<meta property="og:image" content="http://example.com/image/kittens.jpg" />
<meta property="og:title" content="Why kittens are great!" />
...
</head>
<body>
...
Facebook提供sharing debugger,您可以在其中测试您的代码是否按预期运行。如果您无法使其正常工作,您可以通过发布使用opengraph标记的其他网站的链接来查看它应该是什么样子。