该变量在内部定义,get_context_view()因为它需要id访问正确的数据库对象:
class FooView(TemplateView):
def get_context_data(self, id, **kwargs):
---
bar = Bar.objects.get(id=id)
---
def post(self, request, id, *args, **kwargs):
# how to access bar?
# should one call Bar.objects.get(id=id) again?
Run Code Online (Sandbox Code Playgroud)
将bar变量传递给的方式是post()什么?
试图将其保存为FooView的字段并通过进行访问self.bar,但这并不能解决问题。self.bar被看不到post()
您应该扭转它。如果需要barin post(),则需要在其中创建它:
class FooView(TemplateView):
def get_context_data(self, **kwargs):
bar = self.bar
def post(self, request, id, *args, **kwargs):
self.bar = Bar.objects.get(id=id)
...
Run Code Online (Sandbox Code Playgroud)
post()在之前被调用get_context_data,这就是为什么post如果在中定义它就看不到它的原因get_context_data。
| 归档时间: |
|
| 查看次数: |
3032 次 |
| 最近记录: |