Python 3.7 AttributeError:“ str”对象没有属性“ has”

时间:2019-10-26 04:34:39

标签: django python-3.7

使用Python 3.7.3,PIP 19.3.1,Django 2.2,我需要检查对象是否为字符串,并使用以下代码启动它:

if not isinstance(math,  basestring):
    math = latex(math)
    return '<script type="math/tex; mode=display">{}</script>'.format(math)

使用'str'而不是'basestring'会导致脚本输出错误:

'str' object has no attribute 'get' error 

然后我尝试了以下代码

def format_math_display(self, math):
    try:
      if not isinstance(math,  basestring):
         math = latex(math)
         return '<script type="math/tex; mode=display">{} 
                 </script>'.format(math)
    except Exception:
         pass  

但这也会导致一个(其他)错误:

AttributeError at /
'str' object has no attribute 'has'
Request Method:  POST
Request URL:     http://127.0.0.1:8000/
Django Version:  2.2.6
Exception Type:  AttributeError
Exception Value: 'str' object has no attribute 'has'

Python Executable:  C:\Users\Vasundhara\Envs\calculate\Scripts\python.exe
Python Version: 3.7.3
Python Path:    
['C:\\wamp64\\www\\Python3Django2\\Calculate',
 'C:\\Users\\Vasundhara\\Envs\\calculate\\Scripts\\python37.zip',
 'C:\\Users\\Vasundhara\\Envs\\calculate\\DLLs',
 'C:\\Users\\Vasundhara\\Envs\\calculate\\lib',
 'C:\\Users\\Vasundhara\\Envs\\calculate\\Scripts',
 'c:\\users\\vasundhara\\appdata\\local\\programs\\python\\python37\\Lib',
 'c:\\users\\vasundhara\\appdata\\local\\programs\\python\\python37\\DLLs',
 'C:\\Users\\Vasundhara\\Envs\\calculate',
 'C:\\Users\\Vasundhara\\Envs\\calculate\\lib\\site-packages']
Server time:    Sat, 26 Oct 2019 04:08:58 +0000

我在这里想念什么?

0 个答案:

没有答案