【问题标题】:Passing context from get_conext_data to get从 get_context_data 传递上下文以获取
【发布时间】:2021-11-24 11:52:15
【问题描述】:

我曾经在get_context_data 中有book_a_room_form = BookARoomForm(),但后来我将其修改为book_a_room_form = BookARoomForm(initial={'arrival_date': request.session['arrival_date']}) 并将其放入get(请参阅将其移至gethere 的原因)。 get_context_dataget 单独工作。但我不能让他们一起工作。我已经阅读了数十篇关于这个问题的帖子,查看了我的回溯,并收到了各种不同的错误消息。我现在只是在讨论这个问题。

  def get_context_data(self, *args, **kwargs):
           context = super(HotelDetailSlugView, self).get_context_data(*args, **kwargs)
           cart_obj, new_obj = Cart.objects.new_or_get(self.request)
          

        context['hotel_extra_photos'] = AmericanHotelPhoto.objects.all().filter(title=AmericanHotel.objects.all().filter(slug=self.kwargs.get('slug'))[0].id)
        context['room_type'] = RoomType.objects.all().filter(title=AmericanHotel.objects.all().filter(slug=self.kwargs.get('slug'))[0].id)

          

        return context
    def get(self, request, *args, **kwargs):
       # This code is executed each time a GET request is coming on this view
       # It's is the best place where a form can be instantiate

        book_a_room_form  = BookARoomForm(initial={'arrival_date': request.session['arrival_date']})
        
        
        return render(request, self.template_name, {'book_a_room_form': book_a_room_form,
                                                     })

【问题讨论】:

    标签: python django django-views django-forms


    【解决方案1】:

    你应该在 get 中调用上下文

    def get(self, request, *args, **kwargs):
    
           # This code is executed each time a GET request is coming on this view
           # It's is the best place where a form can be instantiate
        
            book_a_room_form  = BookARoomForm(initial={'arrival_date': request.session['arrival_date']})
            
            context = self.get_context_data( *args, **kwargs)
            context['book_a_room_form'] = book_a_room_form
            return render(request, self.template_name, context=context)
                                
    

    【讨论】:

    • 我收到了这个错误信息
    • 文件 "C:\Users\ACER\Documents\Python Environments\Ecommerce\lib\site-packages\django\views\generic\detail.py",第 94 行,在 get_context_data 如果 self.object : AttributeError: 'HotelDetailSlugView' 对象没有属性 'object'
    【解决方案2】:

    感谢 Mohamed Beltagy 的指导。

     def get(self, request, *args, **kwargs):
           # This code is executed each time a GET request is coming on this view
           # It's is the best place where a form can be instantiate
    
          
    
            book_a_room_form  = BookARoomForm(initial={'arrival_date': request.session['arrival_date']})
            
    
            # get_context_data(**kwargs) - ¶
            # Returns context data for displaying the object.
            # The base implementation of this method requires that the self.object attribute be set by the view (even if None). 
            # Be sure to do this if you are using this mixin without one of the built-in views that does so.
            self.object = self.get_object() # assign the object to the view
    
            context = self.get_context_data( *args, **kwargs)
            context['book_a_room_form'] = book_a_room_form
            return render(request, self.template_name, context=context)
    
           
    

    【讨论】:

    • 是的,如果是这样,您需要在文本中调用 get_object
    • 但是如果你注意到它在继承完成时已经在上下文中调用了
    猜你喜欢
    • 2020-11-05
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 2019-10-14
    • 2017-01-15
    相关资源
    最近更新 更多