Home > Enterprise >  Passing context from get_conext_data to get
Passing context from get_conext_data to get

Time:11-24

I use to have book_a_room_form = BookARoomForm() in get_context_data but then I amended it to book_a_room_form = BookARoomForm(initial={'arrival_date': request.session['arrival_date']}) and put it in get (see reason for moving it to get here). get_context_data and get work individually. But I cannot get them to work together. I have read dozens of posts on the issue, looked at my traceback and been given a variety of different error messages. I am now just going round cirlces with the issue.

  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,
                                                     })

CodePudding user response:

you should calling context inside 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)
                            

CodePudding user response:

Thank you Mohamed Beltagy for your guidance.

 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)

       
  • Related