Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

151
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!