Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
getting html from filter and appending in django

Hi I want to know how can I get a data from filter and append it in my html. I am getting this error Uncaught SyntaxError: Unexpected token '<'

HTML

<span id='securedata'></span>
<script>
        window.onscroll = () => {
            if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
                document.querySelector('body').style.background = 'purple';
                <!--my problem is -->
                $('#securedata').append("{% recommend_community_first request.user.pk %}");

            }
            else {
                document.querySelector('body').style.background = 'white';
            }
        }

</script>

templatetags/load_recommendation.py:

@register.inclusion_tag('community/recommend_not_recommend.html')
def recommend_community_first(user_id):
    user = User.objects.get(id=user_id)
    community = Community.objects.all().order_by('-popularity')[:6]
    post = Post.objects.filter(community__in=[i.pk for i in community],video__isnull=True).order_by('-created')
    return {'posts':post,'moi':user}

how can I append that data from my filter without any error. Thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To access Django template variables in JavaScript

Just do like this:

<script>
    let my_url = "{% url 'recommend_community_first' request.user.pk %}";
    // Or if it is {{ }} kind
    let my_var = '{{ context_name_variable }}';

    // Here you can use these variable
    $('#securedata').append(my_url);
</script>

The trick is to always use quote ('' or "") on your variable in the JS section.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda