无法使用烧瓶wsgi呈现模板

时间:2020-08-14 08:42:20

标签: python-3.x flask jinja2 mod-wsgi

我已经在apache2中使用mod_wsgi部署了flask应用程序

现在,检查一切是否按预期进行,看起来像我的渲染模板端点引发了大约500个状态代码错误。

这是我的项目的近似树:

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

我正在呼叫一个应该渲染templates/terms/en/terms.html的端点,并且在日志中得到以下内容:

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

这是我尝试在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
)

我想指定具有JSON正文结果的端点正常运行,上传资源也正常工作,并且获取上传资源正在工作。

只有应渲染模板的端点不起作用,这使我也担心应使用电子邮件模板发送电子邮件的端点

与wsgi一起提供应用程序时,我应该应用任何修复程序吗?

编辑:仅供参考,这是控制器文件夹中的__init__.py

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

1 个答案:

答案 0 :(得分:0)

我将app_folder/templates链接到main_folder,并且现在可以正常运行