Как распечатать все содержимое сессии в Django по шаблону - PullRequest
0 голосов
/ 02 мая 2019

Я пытаюсь распечатать содержимое сеанса по шаблону. Вот мой код:

**views.py:**
def add_to_cart(request,pk,slug):
     product = get_object_or_404( Product, pk=pk,)
     product_document = {
       'title': product.title,
       'price': product.price,
       }
    request.session['cart'][str(product.id)] = product_document
    print(request.session['cart'])
    return render(request,'selling/cart.html')


def cart(request):
    template = loader.get_template("selling/cart.html")
    cart = request.session['cart']
    context ={
        'cart': cart,
    }
    return HttpResponse(template.render(context,request))

**cart.html**
{%if cart%}
 {%for items in cart%}
 <div class="holla3" id="holla3" style="background-image: url({{ product.productimage_set.first.product_images.url }});"></div>
 <div class="alltext" id="textcontainer">
   <p  id="one">{{ product.title}}</p>
   <p  id="two">${{ product.price }}</p>
   <!-- <p id="three">{{ product.product_description }}</p> -->
</div>
{% endfor %}
{% else %}
<h2>There don't seem to be any items in your cart!</h2>
{% endif%}

Итак, в основном я пытаюсь распечатать все элементы из словаря сессий django на шаблоне.

1 Ответ

0 голосов
/ 02 мая 2019

Кажется, вы забыли часть items.

{% if cart %}
   {% for items in cart %}
       <div class="holla3" id="holla3" style="background-image:url({{items.product.productimage_set.first.product_images.url }});"></div>
       <div class="alltext" id="textcontainer">
           <p id="one">{{ items.product.title }}</p>
           <p id="two">${{ items.product.price }}</p>
           <!-- <p id="three">{{ items.product.product_description }}</p> -->
       </div>
   {% endfor %}
{% endif %}

Также вы не должны называть ваши views.py тем же именем, которое вы называете своей переменной внутри него.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...