Home > Net >  Adding dictonary values while using HttpResponseRedirect
Adding dictonary values while using HttpResponseRedirect

Time:11-27

I want to redirect to the existing page and display a queryset which would be determined based on the values submitted via the form. The dictionary values I get from function get_context_data display correctly. The dictionary values I try and add in the post function do not display correctly.

def post(self, request, *args, **kwargs):
    RoomBookingsForm    = BookRoomsForm(request.POST or None)
    self.object = self.get_object() # assign the object to the view
    context = self.get_context_data( *args, **kwargs)

    context.update({'room_type_queryset': RoomType.objects.all().filter()})

    print("all context values")
    print(context)

    if request.method == 'POST':
    
        return HttpResponseRedirect(self.request.path_info, context )
       

As well as context.update I also tried context['room_type_queryset'] = RoomType.objects.all().filter()

Based on the two print statements above I got

all context values
{'object': <AmericanHotel: Caesars Palace>, 'americanhotel': <AmericanHotel: Caesars Palace>, 'slug': 'caesars-palace', 'view': <hotel.views.HotelDetailSlugView object at 0x00000210575117F0>, 'cart': <Cart: 5>, 'hotel_extra_photos': <QuerySet [<AmericanHotelPhoto: Caesars Palace>, <AmericanHotelPhoto: Caesars Palace>, <AmericanHotelPhoto: Caesars Palace>, <AmericanHotelPhoto: Caesars Palace>, <AmericanHotelPhoto: Caesars Palace>, <AmericanHotelPhoto: Caesars Palace>, <AmericanHotelPhoto: Caesars Palace>, <AmericanHotelPhoto: Caesars Palace>, <AmericanHotelPhoto: Caesars Palace>]>, 'room_type': <QuerySet [<RoomType: Caesars Palace, Triple>, <RoomType: Caesars Palace, Double>]>, 'room_type_queryset': <QuerySet [<RoomType: Caesars Palace, Double>, <RoomType: Caesars Palace, Triple>]>}

The results from room_type are correctly displayed in red.

{% for instance in room_type %}
     <H1 style="color:red">{{instance}}</H1>
{% endfor %}

The results from room_type_queryset do not appear.

{% for instance in room_type_queryset %}
    <H1 style="color:blue">{{instance}}</H1>
{% endfor %}

Update

I made a few modifications to my code but based on the screenshot you can see I am still having problems.

enter image description here

   def post(self, request, *args, **kwargs):
            RoomBookingsForm    = BookRoomsForm(request.POST or None)
            self.object = self.get_object() # assign the object to the view
            context = self.get_context_data( *args, **kwargs)
            context['room_type_results'] = RoomType.objects.all().filter()
<h1>Will the results appear</h1>
<h1>{{room_type_results}}</h1>
<p1>{{room_type_results}}</p1>
{{room_type_results}}
<h1>End of will the results appear</h1>

CodePudding user response:

maybe try:

some_form = request.POST.get("form_name")

CodePudding user response:

class gfii6uk56dt4yre tufutuyiyviyxf tttut tfutfjtft tuf utd t

  • Related