Задать вопрос

Тел: +7 965 3737 888

497

Просмотров

1

Ответов

render_to_json

<p><strong>Explanation:</strong></p>
<p>I think this shortcut can be util for who uses JSON many times and does not want to write same code everytime.</p>
<p><strong>Setup:</strong></p>
<p>Saves the snippet as myproject/utils.py or add the code to some place in your project with same ends.</p>
<p><strong>Use in a view:</strong></p>
from myproject.utils import render_to_json

from django.contrib.admin.models import User

def json_view(request):
    admin_user = User.objects.get(username='admin')

    return render_to_json(
        'json/example.json',
        locals(),
    )

<p><strong>Update:</strong></p>
<p>This code can be used as complement to <a href="https://djangosnippets.org/snippets/468/JsonResponse%20snippet">http://www.djangosnippets.org/snippets/154/</a> too.</p>

Вопрос полезен? Да0/Нет0
file_4231.py(307.0б)
None

Ответы (1):

Ответkcarnold:28.02.2008
Ответ полезен? Да0/Нет0

JSON doesn't necessarily mean no cache. Don't blindly copy-paste; think about if your data can be cached.