从 django/python 中的对象打印引用的变量
在下面的Python代码中,对象是否可以知道模板标签正在引用一个变量,并在Python变量中获取它
newemp
是我从视图传递的对象并且模板正在尝试访问一个变量为 {{newemp.get_names.emp_add}}
,现在在 python 代码中对象可以打印这个变量,即 emp_add
class Emp(models.Model):
name = models.CharField(max_length=255, unique=True)
address1 = models.CharField(max_length=255)
def get_names(self):
logging.debug(var)
var=self.some referred object
names = {}
In the below python code ,can the object know that the template tag is referring a variable and get that in a python variable
newemp
is the object that i am passing from the views and the template is trying to access a variable as {{newemp.get_names.emp_add}}
,now in the python code can the object print this variable i.e, emp_add
class Emp(models.Model):
name = models.CharField(max_length=255, unique=True)
address1 = models.CharField(max_length=255)
def get_names(self):
logging.debug(var)
var=self.some referred object
names = {}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不会。一旦从
get_names()
返回适当的对象,访问就会完成,因此没有直接的方法可以知道方法内正在访问什么。No. The access is done once the appropriate object has been returned from
get_names()
so there is no direct way to know within the method what is being accessed.如果您询问是否可以在模板中写入变量,然后在 Python 代码中访问该值,我不相信。恕我直言,这违背了模板的想法。
If you are asking whether you can write to a variable within a template, and then access that value back within your Python code, I do not believe so. That kind of goes against the idea of templates, IMHO.