TypeError: object.__new__() takes no parameters
2012-07-23 17:22
387 查看
代码和错误如下:
>>> from django.template import Template,Context
>>> class Person(object):
... def _init_(self,first_name,last_name):
... self.first_name,self.last_name = first_name,last_name
>>> t=Template('hello,{{person.first_name}}{{person.last_name}}')
>>> c=Context({'person':Person('John','Smith')})
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: object.__new__() takes no parameters
经过折腾发现,因为Context()中对类进行了初始化,而初始化出错了。测试如下:
>>> person=Person('John','Smith')
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: object.__new__() takes no parameters 故有修改如下:
from django.template import Template,Context
>>> class Person(object):
... def _init_(self,first_name,last_name):
... self.first_name,self.last_name = first_name,last_name
>>> t=Template('hello,{{person.first_name}}{{person.last_name}}')
>>> person=Person()
>>> person.first_name='John'
>>> person.last_name='Smith'
>>> c=Context({'person':person})
>>> t.render(c) 结果正确:u'hello,JohnSmith'
>>> from django.template import Template,Context
>>> class Person(object):
... def _init_(self,first_name,last_name):
... self.first_name,self.last_name = first_name,last_name
>>> t=Template('hello,{{person.first_name}}{{person.last_name}}')
>>> c=Context({'person':Person('John','Smith')})
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: object.__new__() takes no parameters
经过折腾发现,因为Context()中对类进行了初始化,而初始化出错了。测试如下:
>>> person=Person('John','Smith')
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: object.__new__() takes no parameters 故有修改如下:
from django.template import Template,Context
>>> class Person(object):
... def _init_(self,first_name,last_name):
... self.first_name,self.last_name = first_name,last_name
>>> t=Template('hello,{{person.first_name}}{{person.last_name}}')
>>> person=Person()
>>> person.first_name='John'
>>> person.last_name='Smith'
>>> c=Context({'person':person})
>>> t.render(c) 结果正确:u'hello,JohnSmith'
相关文章推荐
- __new__方法以及TypeError: object() takes no parameters的处理
- TypeError: object() takes no parameters
- Python3问题TypeError: object() takes no parameters
- python报错 TypeError: object() takes no parameters
- Python_TypeError:object() takes no parameters
- 在Python中TypeError: object() takes no parameters新手问题的解决办法
- TypeError: object() takes no parameters
- TypeError: object() takes no parameters
- python "TypeError: object() takes no parameters"
- TypeError: object.__init__() takes no parameters
- TypeError: object() takes no parameters
- TypeError: object() takes no parameters
- IndentationError: unexpected indent,SyntaxError: invalid syntax,TypeError: object() takes no paramet
- python之TypeError: this constructor takes no arguments
- python 提示错误AttributeError: type object 'str' has no attribute '_name_'
- Spark Reduce TypeError: 'int' object has no attribute '__getitem__
- TypeError: 'generator' object has no attribute '__getitem__'
- Uncaught TypeError: Object #<Object> has no method 'addEvents' ext-all-debug异常
- TypeError: this constructor takes no arguments
- Uncaught TypeError: Object #<HTMLDivElement> has no method 'attr'