Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda