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

152
Views
Render HTML from ajax/json in Django

I am trying to filter blog articles by certain tags via ajax. After days of learning ajax and working on this, I have come up with the below code.

I have successfully got the data filtered by tag and now want to display it as HTML.

I've used the .each loop to iterate through the values in app.js and trying to insert the HTML into the header id trial in all-articles.html.

I am getting a syntax error in the console. The syntax error is on the JSON? data. Here is the screenshot of the error. Any help is appreciated! Thank you enter image description here

all-articles.html

{% for article in page_obj %}
    <article>
      <header id="trial>
        <h4>{{ article.title }}</h4>
      </header>
    </article>
{% endfor %}

app.js

$(document).ready(function () {
  $(".tag-nav-links").on("click", function (e) {
    e.stopPropagation();
    return $.ajax({
      type: "POST",
      url: "",
      data: { filter: `${e.target.textContent}` },
      success: function (json) {
        var html = "";
        $(json).each(function (index, value) {
          html += "<h4>{{" + value.title + "}}</h4>";
        });
        $("#trial").append(html);
      },
    });
  });
});

views.py

@csrf_exempt
def allarticles(request):
    articles_list = Articles.objects.all()
    paginator = Paginator(articles_list, 12)

    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)

    all_tags = Tags.objects.all()

    if request.is_ajax():
        tag_assign = request.POST['filter']
        tag = Tags.objects.get(tag=tag_assign)
        data = serializers.serialize('json', Articles.objects.filter(articletags__tag=tag))
        return JsonResponse(data, safe=False)

    context = {'page_obj': page_obj, 'all_tags': all_tags}
    return render(request, "All-articles.html", context)
about 4 years ago · Juan Pablo Isaza
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!