Search code examples
pythondjangodjango-templatesdjango-class-based-viewsdjango-sessions

how to request session value in template django


i want to fetch a session value directly in myy template i have fom by which i request session and after that i am viewing that session in another html

my views.py where session got created

class Product_detail(View):
    def get(self, request, item_id,):
        item = Item.objects.filter(id=item_id)
        category_list = Categories.objects.all()
        items = Item.objects.order_by('-update_at')
        return render (request, 'product_detail.html',{"items" : item, 'category_list': category_list, 'item': items })

    def post(self, request, item_id):
        item = request.POST.get('item')
        size = request.POST.get('Size')
        cart = request.session.get('cart')
        if cart:
            cart[item] = size
        else:
            cart = {}
            cart[item] = size
        request.session['cart'] = cart
        print(request.session['cart'])
        return redirect('products:detail', item_id=item_id)

my views.py in which i want ot render session

class Cart(View):
    def get (self, request): 
        cart = request.session.get('cart', None)
        if not cart:
            cart = {} # or however you define your empty cart
        request.session['cart'] = cart
        ids = (list(cart.keys()))
        ids = (list(request.session.get('cart').keys()))
        item = Item.get_items_by_id(ids)
        print(item)
        return render(request, 'cart.html', {'items': item })

my html code

 <tbody style="margin-bottom: 20px;">
                              {% for item in items %}
                              <tr>
                                <th scope="row">{{forloop.counter}}</th>
                                <td> <img src="{{item.first.url}}" alt="" height="100px"></td>
                                <td>{{item.name}}</td>
                                  {% for key, value in request.session.cart.items %}
                                      {% if value == value %}
                                        <td>{{ value }}</td> 
                                      {% endif %}
                                  {% endfor %}
                                <td>{{item.price|currency}}</td>
                                <td> <a href="#">Remove</a> </td>
                              </tr>
                              {% endfor %}
                            </tbody>

so my session list has two values in it one is id and other is its size so want to show user it selected size right now it show both the id and size but i want to render only user selected size

any idea how to achieve that

filter

@register.filter
def item_size(cart , item):
    return cart[item]

it look like this


Solution

  • so for requesting the session value you can not directly render it you have to create filter or that an then render it so here in my case i want to render the product size which is also it's value so in my templatetag folder i created a filter

    my filter for fetching values of session

    @register.filter(name='cart_size')
    def cart_size(item, cart):
        keys = cart.keys()
        for id in keys:
            if int(id) == item.id:
                return cart.get(id)
        return 'Regular';
    

    and in my template for rendering

    {{item|cart_size:request.session.cart}}
    

    so what it does as mentioned in question item id is key and size its value and i assigned the size to its item so what filter does it is looking for keys in cart and as you know the key is id so i assign id is equal to item.id and then it return the size which assigned with id and then in template i call it like this because all the value in cart so i am requesting that too otherwise it will throw error