[Fixed]-Relative Template Import

1👍

If that’s the way your templates are set up, you should just be able to reference it by name

loader.render_to_string('mytemplate.html', {
    'username': user.name,
})

However be aware that in case you have any name conflicts within your Django project (or even if you don’t), it’s better to have a directory that mirrors the app name within your templates directory. Your tree structure for an app would look like this:

appname/
    some_file.py
    templates/
        appname/
            mytemplate.html

Then you would just reference it as loader.render_to_string('appname/mytemplate.html', {...})

Leave a comment