S
S
Sergey08082017-10-24 13:44:38
Django
Sergey0808, 2017-10-24 13:44:38

Python Django how to make share buttons using templatetags for all apps?

I create a templatetags folder in the application. Next, I create a share_social file in this folder with the following content:

from django import template
    from django.template.loader import render_to_string

    register = template.Library()

    @register.simple_tag(name='share')
    def share():
        return render_to_string('news/social_buttons.html')

Next, I load this tag into the template and use it:
{% load share_social %}
    <div class="share-social">
        <h3>Поделиться в социальных сетях:</h3>
            {% share %}
    </div>

In news models, I implement the get_absolute_url method, which returns a link to the post.
The content of the loaded template by the {% share %} tag:
<a href="http://vk.com/share.php?url=http:/{% url 'news:index' %}/{{ news.get_absolute_url }}"
   target="_blank" rel="nofollow">
       <img src="/media/social_share/vk-social-logotype_318-65222.jpg">
   </a>
   <a href="https://twitter.com/share?url={% url 'news:index' %} + /{{ news.get_absolute_url }}"
   target="_blank" rel="nofollow">
      <img src="/media/social_share/twitter-logo-on-black-background_318-76332.jpg">
   </a>
   <a href="https://www.facebook.com/sharer/sharer.php?u={% url 'news:index' %}/{{ news.get_absolute_url }}"
   target="_blank" rel="nofollow">
      <img src="/media/social_share/facebook-logo_318-49940.jpg">
   </a>
   <a href="http://www.odnoklassniki.ru/dk?st.cmd=addShare&st.s=1&st._surl={% url 'news:index' %}/{{ news.get_absolute_url 
   }}"
   target="_blank" rel="nofollow">
      <img src="/media/social_share/odnoklassniki-logo_318-65358.jpg">
   </a>

The link is not created, how to transfer the object to the link? Or suggest another approach

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Astrohas, 2017-10-24
@Astrohas

use include instead of bicycles

<div class="share-social">
        <h3>Поделиться в социальных сетях:</h3>
            {% include "news/social_buttons.html" %}
    </div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question