Skip to content

Instantly share code, notes, and snippets.

@pydanny
Last active June 8, 2020 18:07
Show Gist options
  • Save pydanny/3c866ce8b4566d8627e5aa995f0134b2 to your computer and use it in GitHub Desktop.
Save pydanny/3c866ce8b4566d8627e5aa995f0134b2 to your computer and use it in GitHub Desktop.
simple_async_update_view.py 
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render, redirect
from django.views.generic import View
from asgiref.sync import sync_to_async
class AsyncViewMixin:
async def __call__(self):
return super().__call__(self)
class SimpleBookUpdateView(LoginRequiredMixin, AsyncViewMixin, View):
def get(self, request, *args, **kwargs):
return render(request, "test.html", {"book": Book.objects.first()})
def post(self, request, *args, **kwargs):
book = Book.objects.first()
book.title += "1"
sync_to_async(book.save())
return redirect("books:simple")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment