radiac / nanodjango

Full Django in a single file - views, models, API ,with async support. Automatically convert it to a full project.
573 stars 25 forks source link

Improve embedding html and templates #44

Open radiac opened 3 weeks ago

radiac commented 3 weeks ago

Two related issues:

I think these can be addressed in one solution:

Suggested syntax:

app = Django()
@app.route("/")
def index(request):
    return app.render("index.html", context=...)

app.templates["index.html"] = "{% extends 'base.html' %}{% block content %}Hello {{ target }}{% endblock %}"

Build this using or on top of django.template.loaders.locmem.Loader, and add this as the priority first loader in settings

Here app.render would be a wrapper for django.shortcuts.render, which saves an import and inserts the current request object - I think that should be available in the app. If not, maybe importing django.shortcuts.render would make more sense.

When we run convert, it can then write these templates out into text files, and rewrite app.render into a straight call to render. It will also need to remove our loader from the settings.