首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >只有在我们添加了所有app_rules之后,错误处理程序导入才能工作

只有在我们添加了所有app_rules之后,错误处理程序导入才能工作
EN

Stack Overflow用户
提问于 2014-07-21 17:07:25
回答 1查看 107关注 0票数 0

为什么代码被标记为不工作、不工作并抛出相应的错误?什么是正确的方法呢?import *不是正确的方法。

我的routing.py

代码语言:javascript
复制
import os
from flask import Flask
from views import UserView

#App Config
app= Flask(__name__)

# ********************** NOT WORKING ****************************
# from error_handlers import auth_error ==> AssertionError: View function mapping is overwriting an existing endpoint function: user_view 
# from error_handlers import * ==> AssertionError: View function mapping is overwriting an existing endpoint function: user_view
# ***************************************************************

#URLs
app.add_url_rule('/users', view_func=UserView.as_view('user_view'), methods=['GET'])

# ********************** NOT WORKING ****************************
# from error_handlers import auth_error ==> ImportError: cannot import name auth_error
# ***************************************************************

# ********************** WORKING ****************************
from error_handlers import *
# ***************************************************************

if __name__ == '__main__':
    app.run(debug=True, host = '0.0.0.0', port=5050)

error_handlers.py是:

代码语言:javascript
复制
from flask import render_template, jsonify

from routing import app
from myexceptions import *

#Error Handlers

@app.errorhandler(404)
def unexpected_error(error):
    """ error handler for unknown error """
    return render_template('error.html'), 404


@app.errorhandler(AuthenticationException)
def auth_error(error):
    """ error handler user entered data exception """
    return jsonify({'error': error.get_message()})

views.py是:

代码语言:javascript
复制
from flask.views import View
from flask import render_template

from myexceptions import AuthenticationException

class ParentView(View):
    def get_template_name(self):
        raise NotImplementedError()

    def render_template(self, context):
        return render_template(self.get_template_name(), **context)

    def dispatch_request(self):
        context = self.get_objects()
        return self.render_template(context)

class UserView(ParentView):
    def get_template_name(self):
        raise AuthenticationException('test')
        return 'users.html'

    def get_objects(self):
        return {}

AuthenticationException只是一个在myexceptions中定义的Exception子类。

EN

Stack Overflow用户

发布于 2014-07-26 17:37:21

我也读过你的代码在你的github之前,也许错误是:你导入的一些文件中的应用程序不使用应用程序

我的修改代码:

routing.py

代码语言:javascript
复制
import os
from flask import Flask


#App Config
app= Flask(__name__)

from views import UserView
app.add_url_rule('/users', view_func=UserView.as_view('user_view'), methods=['GET'])


if __name__ == '__main__':
    print app.url_map
    app.run(debug=True, host = '10.210.71.145', port=5050)

你的error_handle.py可能是这样的:

代码语言:javascript
复制
from flask import render_template, jsonify

from myexceptions import AuthenticationException


@app.errorhandler(404)
def unexpected_error(error):
    """ error handler for unknown error """
    return render_template('error.html'), 404


@app.errorhandler(AuthenticationException)
def auth_error(error):
    """ error handler user entered data exception """
    return jsonify({'error': error.get_message()})
票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24861330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档