File: views.py

package info (click to toggle)
python-django-debug-toolbar 1%3A6.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,052 kB
  • sloc: python: 7,555; javascript: 636; makefile: 67; sh: 16
file content (42 lines) | stat: -rw-r--r-- 1,081 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import asyncio

from asgiref.sync import sync_to_async
from django.contrib.auth.models import User
from django.http import JsonResponse
from django.shortcuts import render


def increment(request):
    try:
        value = int(request.session.get("value", 0)) + 1
    except ValueError:
        value = 1
    request.session["value"] = value
    return JsonResponse({"value": value})


def jinja2_view(request):
    return render(request, "index.jinja", {"foo": "bar"}, using="jinja2")


async def async_home(request):
    return await sync_to_async(render)(request, "index.html")


async def async_db(request):
    user_count = await User.objects.acount()

    return await sync_to_async(render)(
        request, "async_db.html", {"user_count": user_count}
    )


async def async_db_concurrent(request):
    # Do database queries concurrently
    (user_count, _) = await asyncio.gather(
        User.objects.acount(), User.objects.filter(username="test").acount()
    )

    return await sync_to_async(render)(
        request, "async_db.html", {"user_count": user_count}
    )