6👍
✅
Try Django’s render_to_string
:
economy = render_to_string('dbmanager/classes.html', {"classes": economy_classes})
business = render_to_string('dbmanager/classes.html', {"classes": business_classes})
render_to_string()
loads a template, renders it and then returns the resulting string. You can then send these resulting strings as JSON.
Your final code now becomes:
from django.template.loader import render_to_string
def getClasses(request):
User = request.user
aircomcode = request.POST.get('aircompany_choice', False)
working_row = Pr_Aircompany.objects.get(user=User, aircomcode=aircomcode)
economy_classes = working_row.economy_class
business_classes = working_row.business_class
economy = render_to_string('dbmanager/classes.html', {"classes": economy_classes})
business = render_to_string('dbmanager/classes.html', {"classes": business_classes})
return JsonResponse({"economy": economy,
"business": business})
2👍
render_to_response
is, as the name implies, for rendering a response. You don’t want to do that; you want to render two templates, and put them into a JSON response. So use render_to_string
.
- [Django]-Make a One to Many Relation in django
- [Django]-Mod_wsgi (3.4-14) / Apache 2.4.12 / Red Hat (6.7) / Django 1.8.2 hanging under load
- [Django]-How to check if, elif, else conditions at the same time in django template
- [Django]-Slow Celery Task Times
- [Django]-Preserving ModelAdmin attributes (list_display, etc.) when inheriting from a custom model
- [Django]-Django application deployed at suburl, redirect to home page after login
- [Django]-Django: Store Hierarchical Data
- [Django]-How to translate Enum string in django?
Source:stackexchange.com