G
G
Golopolos2020-06-29 11:03:45
Django
Golopolos, 2020-06-29 11:03:45

How to get json of whole page?

Good day.

Tell me how to get any url in django as json?
Let's say we add ?template=1 to any url at the end
so that it displays something like this

{
  "template": "<div>тут мой шаблон</div>"
}


Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-06-29
@Golopolos

If you need the whole view, then you can do it like this, but here you are not passing the name of the template, but the URL

from django.test import Client
client = Client()
response = client.get('/')
content = response.content

content is placed in json.
If you need to punish the template, then we use this scheme
from django.template.loader import get_template
template = get_template('my_template.html')
result = template.render({'var': 'test'})

and result is placed in json. But the disadvantage of this approach is that you need to collect context

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question