-1

I have a django blog on the web and I finally put the facebook share plugin in the one. But everytime I share, I can share the same image, the image is a static image from the head of base.html. I am using django 1.10,python3.6 and no one share plugin. How can I share respective image from my respective page? Very thanks! ps: I think any others code is be unnecessary.

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)

and 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&amp;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 %}

Bye.

victorcd
  • 429
  • 2
  • 6
  • 17
  • 1
    Add opengraph `` tags in your articles' html head. `og:image` will be used in the facebook feed. Facebook provides both documentation and webmaster tools to test it out. https://developers.facebook.com/docs/sharing/webmasters – Håken Lid Aug 30 '17 at 17:37
  • How can I do this? Inside the posts or have I to chance html code? I'm sorry, it is my first blog. I going to read the documentation – victorcd Aug 30 '17 at 19:52
  • 1
    You have to add those tags inside the html head tag. This is similar to having individual `title` tags for each blog post. There's lots of documentation about this at https://developers.facebook.com – Håken Lid Aug 30 '17 at 19:55
  • So, I'm trying this: withou sucess. – victorcd Aug 30 '17 at 21:51

1 Answers1

1

The recommended way to choose a sharing image is to add the relevant meta opengraph og:image tags to your html head. It should look something like this. Use django template code to insert the correct content in each meta tag.

<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 provides a sharing debugger where you can test that your tags are working as expected. If you have trouble making it work, you can see what it's supposed to look like by posting links from other sites that use opengraph tags.

Håken Lid
  • 22,318
  • 9
  • 52
  • 67
  • but I 'm sharing a dynamic post, so I can't do this way. Therefore I 'm using " ", I think in this way I can call the objects corretly, but I 'm doing something wrong – victorcd Aug 31 '17 at 00:54