File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed
django_async_extensions/aforms Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 4
4
5
5
from django .forms .models import ModelForm
6
6
7
+ from django_async_extensions .aforms .utils import AsyncRenderableFormMixin
7
8
8
- class AsyncModelForm (ModelForm ):
9
+
10
+ class AsyncModelForm (AsyncRenderableFormMixin , ModelForm ):
9
11
@classmethod
10
12
async def from_async (cls , * args , ** kwargs ):
11
13
return await sync_to_async (cls )(* args , ** kwargs )
Original file line number Diff line number Diff line change
1
+ from asgiref .sync import sync_to_async
2
+
3
+ from django .utils .safestring import mark_safe
4
+
5
+
6
+ class AsyncRenderableMixin :
7
+ async def arender (self , template_name = None , context = None , renderer = None ):
8
+ renderer = renderer or self .renderer
9
+ template = template_name or self .template_name
10
+ context = context or self .get_context ()
11
+ return mark_safe ( # noqa:S308
12
+ await sync_to_async (renderer .render )(template , context )
13
+ )
14
+
15
+
16
+ class AsyncRenderableFormMixin (AsyncRenderableMixin ):
17
+ async def aas_p (self ):
18
+ """Render as <p> elements."""
19
+ return await self .arender (self .template_name_p )
20
+
21
+ async def aas_table (self ):
22
+ """Render as <tr> elements excluding the surrounding <table> tag."""
23
+ return await self .arender (self .template_name_table )
24
+
25
+ async def aas_ul (self ):
26
+ """Render as <li> elements excluding the surrounding <ul> tag."""
27
+ return await self .arender (self .template_name_ul )
28
+
29
+ async def aas_div (self ):
30
+ """Render as <div> elements."""
31
+ return await self .arender (self .template_name_div )
You can’t perform that action at this time.
0 commit comments