-1

I have deployed a flask application, using mod_wsgi with apache2

Now, checking if everything works as intended, it looks like my endpoints which render templates are provoking some 500 status code errors.

Here's an approximative tree of my project:

main_folder
  requirements.txt
  mainfile.wsgi
  app_folder
    controllers
      views.py
    models
    repository
    services
    static
    templates
      terms
        en
          terms.html
        fr
          terms.html
    uploads
    __init__.py
    config.cfg
    webapp.py

I am calling an endpoint which is supposed to render the templates/terms/en/terms.html and I get the following in the logs:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1820, in handle_user_exception
     reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/dist-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/dist-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/var/www/main_folder/app_folder/controllers/views.py", line 33, in gts
    return render_template('terms/' + lang + '/terms.html')
  File "/usr/local/lib/python3.7/dist-packages/flask/templating.py", line 138, in render_template     
    ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "/usr/local/lib/python3.7/dist-packages/jinja2/environment.py", line 869, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "/usr/local/lib/python3.7/dist-packages/jinja2/environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/usr/local/lib/python3.7/dist-packages/jinja2/environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "/usr/local/lib/python3.7/dist-packages/jinja2/loaders.py", line 113, in load
    source, filename, uptodate = self.get_source(environment, name)
  File "/usr/local/lib/python3.7/dist-packages/flask/templating.py", line 60, in get_source
    return self._get_source_fast(environment, template)
  File "/usr/local/lib/python3.7/dist-packages/flask/templating.py", line 89, in _get_source_fast
    raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: terms/en/terms.html

Here's an excerp of the code where I try to render the template in app_folder/controllers/views.py:

from flask import render_template, request, send_file

from app_folder.controllers import Controllers as controllers

def gts(lang='fr'):
    return render_template('terms/' + lang + '/terms.html')

controllers().register(
    '/cgu/<string:lang>',
    'cgu_lang',
    gts
)

I want to specify that endpoints with JSON body results are functionning, uploading ressources are working as well, and getting an uploaded ressource is working.

Only endpoints which should render templates are not working, which is getting me worried as well for endpoints which should send emails using an email template

Is there any fix I should apply when serving the application with wsgi?

Edit: FYI, this is the __init__.py in the controllers folder

import pkgutil


METHODS = set([
    'GET',
    'POST',
    'PUT',
    'PATCH',
    'DELETE',
    'COPY',
    'HEAD',
    'OPTIONS',
    'LINK',
    'UNLINK',
    'PURGE',
    'LOCK',
    'UNLOCK',
    'PROPFIND',
    'VIEW'
])


class Controllers:

    class __OnlyOne:

        # ======================================================
        #           the code goes here
        # =======================================================

        def __init__(self):
            self.val = None
            self.rules = []

        def __str__(self):
            return 'self' + self.val

        def register(self, rule, view_name, view_func):
            r = {}
            r["rule"] = rule
            r["view_name"] = view_name
            r["view_func"] = view_func

            self.rules.append(r)

        def register_methods(self, rule, view_name, view_func, methods=METHODS):
            r = {}
            r["rule"] = rule
            r["view_name"] = view_name
            r["view_func"] = view_func
            r["methods"] = methods

            self.rules.append(r)

        def grab(self, app):
            for r in self.rules:
                if "methods" not in r.keys():
                    app.add_url_rule(r["rule"], r["view_name"], r["view_func"])
                else:
                    app.add_url_rule(r["rule"], r["view_name"],
                                     r["view_func"], methods=r["methods"])

        # =================================================================
        #           the code goes up there
        # ==================================================================

    instance = None

    def __new__(cls):  # __new__ always a classmethod
        if not Controllers.instance:
            Controllers.instance = Controllers.__OnlyOne()
        return Controllers.instance

    def __getattr__(self, name):
        return getattr(self.instance, name)

    def __setattr__(self, name):
        return setattr(self.instance, name)


# import all the modules in folder
__all__ = []
for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
    __all__.append(module_name)
    _module = loader.find_module(module_name).load_module(module_name)
    globals()[module_name] = _module
prout
  • 317
  • 5
  • 18
  • for `lang='fr'` i could see no template in the folder – KcH Aug 14 '20 at 09:17
  • @Codenewbie there is but I didn't add it to my tree in the question – prout Aug 14 '20 at 09:26
  • glad u found the solution --- always remember the error `raise TemplateNotFound(template)` which means template actually doesn't exist or not found in the relative path ... – KcH Aug 14 '20 at 13:22
  • the thing is, I couldn't debug the thing as it was on a remote server, and of course, when I launch a dev server using `flask run` it works because it doesn't really need the wsgi layer, so it's run from the `app_folder` where the `templates` folder is – prout Aug 14 '20 at 14:37

1 Answers1

0

I symlinked my app_folder/templates into my main_folder and it's now working

prout
  • 317
  • 5
  • 18