简体   繁体   English

龙卷风/ Python –如何避免这种重复?

[英]Tornado/Python – How do I avoid this repetition?

I am new to Tornado, and I have this simplified code for the purposes of this question: 我是Tornado的新手,出于这个问题,我有以下简化代码:

class LoginHandler(BaseHandler):
    def get(self):
        error_message = None
        title = "Log in to your account"

        self.render("login.html", error_message=error_message, title=title)

    def post(self):
        #function and params excluded for brevity of question
        error_message = self.authenticate_user()
        title = "Log in to your account"

        self.render("login.html", error_message=error_message, title=title)

The self.render("login.html", error_message=error_message, title = title) as well as the title variable are repeated (seemingly unnecessarily) because otherwise, I get the error "Global variable 'title' or 'error_message' not defined," depending on whether I use post or get to render the page. self.render("login.html", error_message=error_message, title = title)和title变量(看似不必要),因为否则,我得到的错误是“未定义全局变量'title'或'error_message' ”,具体取决于我使用post还是get网页。

I have a different title for every page, and I was wondering how I can simply have one title variable and one self.render("login.html"...) per page handler (ie, LoginHandler ) that will work when either the get or post function is called. 我为每个页面使用了不同的标题,我想知道如何在每个页面处理程序(即LoginHandler )中简单地拥有一个title变量和一个self.render("login.html"...) ,当调用get或post函数。 I don't like the verbatim repetition, but I am having trouble avoiding error messages when I don't do the same thing in both functions. 我不喜欢逐字记录重复,但是当我在两个函数中都没有做相同的事情时,我很难避免出现错误消息。

How can I solve this? 我该如何解决? Thank you. 谢谢。

You can avoid redeclaring the title and error_message variables by initiating them as class members. 通过将它们初始化为类成员,可以避免重新声明titleerror_message变量。 (I used the leading underscore _ in the variable name to indicate that this value should be private and is only to be used in this class.) (我在变量名中使用了下划线_来指示此值应为私有值,并且只能在此类中使用。)

class LoginHandler(BaseHandler):
    def __init__(self):
        # Call the BaseHandler's __init__ function to initialize parent's members
        BaseHandler.__init__()
        self._title = "Log in to your account"

    def get(self):
        self.render("login.html", error_message=None, title=self._title)

    def post(self):
        self.render("login.html", error_message=self.authenticate_user(), title=self._title)

The added advantages of doing it this way is that you only need to change the title in one spot and you don't run the risk of getting a different title depending on whether the method was get or post . 这样做的额外好处是,您只需要在一个位置更改title ,而不必冒根据方法是get还是post获得另一个标题的风险。

NOTE: It appears that in error_message is not necessary - it's only being used in a single case. 注意:似乎在error_message中不是必需的-仅在一种情况下使用。 The self.render() calls do not receive the same parameters and therefore are both necessary. self.render()调用不会接收相同的参数,因此都是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM