Django:Views.py需要访问另一个python文件中的Class和Function

时间:2017-09-19 14:10:52

标签: python django

在我的helper.py中我有一个类和一个函数定义:

class Suites():
    NAME_SUITE = ['NATE', 'TOM', 'TED', 'JAN', 'SAM']

def check_suite(name, suite):
    my_suite = Suites[suite] #I want my_suite to = ['NATE', 'TOM', 'TED', 'JAN', 'SAM']
    return name in suite #I want to return True or False

并在views.py中我试图找出user_in_suite为True

 from .helper import check_suite
 from .helper import Suites

 def apps(request):
    user_name = 'NATE'
    suite_to_check = 'NAME_SUITE'
    user_in_suite = check_suite(name=user_name, suite=suite_to_check)

但我收到了helper.py的错误:'classobj'对象没有属性'__getitem __

1 个答案:

答案 0 :(得分:1)

def check_suite(name, suite):
    my_suite = Suites.__dict__[suite] # __dict__ gives you access to the dictionary of class.
    return name in my_suite

这可以解决您的问题。