简体   繁体   English

Django:将变量从 get_context_data() 传递到 post()

[英]Django: Passing variable from get_context_data() to post()

The variable is defined inside get_context_view() since it requires an id to access correct database object:该变量在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?

What would be the way to pass bar variable to post() ?bar变量传递给post()什么?

Tried to save it as FooView's field and access it via self.bar , but this doesn't do the trick.试图将其保存为self.bar的字段并通过self.bar访问它,但这并不能解决问题。 self.bar is not seen by post() post()看不到self.bar

You should reverse it.你应该扭转它。 If you need bar in post() , you need to create it there:如果你需要在post() bar ,你需要在那里创建它:

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)
        ...

post() is called before get_context_data , that's why post doesn't see it if you define it in get_context_data . post()get_context_data之前被调用,这就是为什么如果你在get_context_data定义post就看不到它。

As knbk said, my solution was the dispatch method, there I define the variables that I will use in get_context_data and in post methods, that worked for me as a way to share data here my example正如 knbk 所说,我的解决方案是 dispatch 方法,在那里我定义了我将在 get_context_data 和 post 方法中使用的变量,这对我来说是在我的示例中共享数据的一种方式

def dispatch(self, request, *args, **kwargs):

    self.some_data = '123'
    if request.method.lower() in self.http_method_names:
        handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
    else:
        handler = self.http_method_not_allowed
    return handler(request, *args, **kwargs)

def get_context_data(self, **kwargs):
    context = super(MyTestCreateView, self).get_context_data(**kwargs)
    print('test get_context_data', self.some_data)

def post(self, request, *args, **kwargs):        
    print('test post', self.some_data)
    return super().post(request, *args, **kwargs)

Classy CBV I hope it helps 经典的 CBV我希望它有帮助

If the variable is defined in your CBV (Class Based View), it can directly be called in your template.如果在您的 CBV(基于类的视图)中定义了该变量,则可以直接在您的模板中调用它。

Views.py视图.py

class SomeRandomView(FormView):
    username = 'SomeValue'

Template.html模板.html

<p>Username is {{view.username}}<p>

Sidenote: You can even call the methods in your view class directly within the template file which is after version 1.5 Django if I could recall correctly.旁注:如果我没记错的话,您甚至可以直接在 Django 1.5 版之后的模板文件中调用视图类中的方法。

I know its late, but I faced this same problem few days ago.我知道为时已晚,但几天前我遇到了同样的问题。 However here is my solution.但是,这是我的解决方案。

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['foos'] = self.object.filter()
    return context

def post(self, request, *args, **kwargs):
    context = super().post(request, *args, **kwargs)
    foos = self.get_context_data().get('foos')
    # do stuff here
    return context

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM