38👍
✅
You can leverage Django’s template loader to render your template, including whatever context you pass to it, as a string and then save that out to the filesystem. If you need to save that file on an external system, such as Amazon S3, you can use the Boto library.
Here’s an example of how to render a view to a file, using an optional querystring parameter as the trigger…
from django.shortcuts import render
from django.template.loader import render_to_string
def my_view(request):
as_file = request.GET.get('as_file')
context = {'some_key': 'some_value'}
if as_file:
content = render_to_string('your-template.html', context)
with open('path/to/your-template-static.html', 'w') as static_file:
static_file.write(content)
return render('your-template.html', context)
6👍
django-bakery is a well-developed “set of helpers for baking your Django site out as flat files”.
👤the
- [Django]-Django get_or_create fails to set field when used with iexact
- [Django]-How to access the user profile in a Django template?
- [Django]-How to get GET request values in Django?
- [Django]-Django: Check if settings variable is set
- [Django]-How can I enable CORS on Django REST Framework
- [Django]-Django admin login suddenly demanding csrf token
Source:stackexchange.com