I
I
IIITRIX2020-07-12 16:51:24
Django
IIITRIX, 2020-07-12 16:51:24

How to fetch json?

Tell me how to discard unnecessary data received in json?
I am getting full page in json like this

def ArticlesListView(request):
  context = {
    'articles': Article.objects.get_article()
  }
  if request.GET.get("mode"):
    data = {
        "html": render_to_string("articles/list.html", context)
      }
    }
    data = json.dumps(data, ensure_ascii=False).replace('/', r'\/')
    return HttpResponse(data, content_type="application/json")
  return render(request, "articles/list.html", context)


This is how I get the entire page with DOCTYPE (because list.html has extends base.html). But I only need a certain block, with assigned for example
<div id='json'></div>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2020-07-12
@IIITRIX

Move this block into a separate template. And it’s not clear why it’s so perverse and wrap it all in json, when you can simply return this piece of html as it is.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question