I am building a django site where peope would be able to make appointments and also buy some products. so i have created to applications in my project, one is called myapp and the other is called store. i have an issue with my store app,
Traceback (most recent call last):
File “C:\Users\Mukelabai\Desktop\EXPERIENCE\env\Lib\site-packages\django\core\handlers\exception.py”, line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Mukelabai\Desktop\EXPERIENCE\env\Lib\site-packages\django\utils\deprecation.py”, line 136, in call
response = self.process_response(request, response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Mukelabai\Desktop\EXPERIENCE\env\Lib\site-packages\django\middleware\clickjacking.py”, line 27, in process_response
if response.get(“X-Frame-Options”) is not None:
^^^^^^^^^^^^
AttributeError: ‘Product’ object has no attribute ‘get’
my models
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.DecimalField(max_digits=8, decimal_places=2)
description = models.TextField()
quantity = models.PositiveIntegerField(default=1)
def __str__(self):
return self.name
my views
from django.shortcuts import redirect, render
from .models import Product
def product(request):
products = Product.objects.all()
return render(request, 'product-4col.html', {'products': products})
def cart(request, product_id):
product = Product.objects.get(pk=product_id)
cart = request.session.get('cart', {})
cart_item = cart.get(product_id)
if cart_item:
cart_item['quantity'] += 1
else:
cart_item = {'quantity': 1, 'product': product}
cart[product_id] = cart_item
request.session['cart'] = cart
return redirect('store:cart')
def view_cart(request):
cart = request.session.get('cart', {})
cart_items = []
total_price = 0
for product_id, cart_item in cart.items():
product = Product.objects.get(pk=product_id)
quantity = cart_item['quantity']
total_price += product.price * quantity
cart_items.append({'product': product, 'quantity': quantity})
return render(request, 'cart.html', {'cart_items': cart_items, 'total_price': total_price})
def checkout(request):
if request.method == 'POST':
request.session['cart'] = {}
return redirect('checkout_complete')
return render(request, 'checkout.html')
my urls
from django import views
from django.urls import path
from .views import *
urlpatterns = [
path('cart/<int:product_id>/', cart, name='cart'),
path('checkout/', checkout, name='checkout'),
path('product/', Product, name='product'),
path('view_cart/', view_cart, name='view_cart'),
]
my html snippet
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<!-- SITE TITTLE -->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CL Experience</title>
<!-- GOOGLE FONT -->
<link href="https://fonts.googleapis.com/css2?family=Herr+Von+Muellerhoff&family=Montserrat:wght@400;700&family=Open+Sans:wght@300;400;600;700&display=swap" rel="stylesheet">
<!-- PLUGINS CSS STYLE -->
<link href="{% static 'plugins/bootstrap/css/bootstrap.min.css'%}" rel="stylesheet">
<link href="{% static 'plugins/font-awesome/css/font-awesome.min.css'%}" rel="stylesheet">
<link href="{% static 'plugins/animate/animate.css'%}" rel="stylesheet">
<link href="{% static 'plugins/selectbox/select_option1.css'%}" rel="stylesheet">
<link href="{% static 'plugins/owl-carousel/owl.carousel.min.css'%}" rel="stylesheet" media="screen">
<link href="{% static 'plugins/fancybox/jquery.fancybox.min.css'%}" rel="stylesheet">
<link href="{% static 'plugins/isotope/isotope.min.css' %}" rel="stylesheet">
<link href="{% static 'plugins/datepicker/datepicker.min.css'%}" rel="stylesheet">
<!-- CUSTOM CSS -->
<link href="{% static 'css/style.css' %}" rel="stylesheet">
<link href="{% static 'css/rtl.css' %}" rel="stylesheet" id="rtl_css">
<link href="{% static 'css/default.css'%}" rel="stylesheet" id="option_color">
<link href="{% static 'css/style.rtl.css'%}" rel="stylesheet">
<link href="{% static 'css/color-option2.css'%}" rel="stylesheet">
<link href="{% static 'css/color-option3.css' %}" rel="stylesheet">
<link href="{% static 'css/color-option4.css' %}" rel="stylesheet">
<!-- FAVICON -->
<link href="{% static 'img/favicon.png' %}" rel="shortcut icon">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<style>
/* Paste this css to your style sheet file or under head tag */
/* This only works with JavaScript,
if it's not present, don't show loader */
.no-js #loader {
display: none;
}
.js #loader {
display: block;
position: absolute;
left: 100px; top: 0;
}
.se-pre-con {
position: fixed;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 9999;
background: url('/static/plugins/simple-pre-loader/images/loader-64x/Preloader_2.gif') center no-repeat #fff;
}
</style>
<!--Include jQuery library -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(window).on("load", function() {
$(".se-pre-con").fadeOut("slow");
});
</script>
</head>
<!--
<script>
//paste this code under head tag or in a seperate js file.
// Wait for window load
$(window).load(function() {
// Animate loader off screen
$(".se-pre-con").fadeOut("slow");
});
</script>
-->
<body id="body" class="body-wrapper static">
<div class="se-pre-con"></div>
<div class="main-wrapper">
<!-- HEADER -->
<header id="pageTop" class="header">
<!-- TOP INFO BAR -->
<div class="top-info-bar">
<div class="container">
<div class="top-bar-right">
<ul class="list-inline">
<li><a href=""><i class="fa fa-envelope" aria-hidden="true"></i> info@cl.com</a></li>
<li><span><i class="fa fa-phone" aria-hidden="true"></i>+1 234 567 8900</span></li>
</ul>
</div>
</div>
</div>
<!-- NAVBAR-->
<nav class="navbar navbar-expand-md main-nav navbar-light">
<div class="container">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html"><img class="lazyestload" data-src="{% static 'img/logo.png'%}" src="{% static 'img/logo.png'%}" alt="logo"></a>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav ml-auto">
<li class="nav-item active">
<a class="nav-link" href="{% url 'home'%}">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" href="javascript:void(0)">Services</a>
<ul class="dropdown-menu">
<li><a href="{% url 'services'%}">Services</a></li>
<!--<li><a href="single-service.html">Service Details</a></li> -->
</ul>
</li>
<li class=" dropdown megaDropMenu nav-item " >
<a class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" href="javascript:void(0)">Store</a>
<ul class="row dropdown-menu justify-content-md-between">
<li class="">
<ul class="list-unstyled">
<li><a href="product-right-sidebar.html">Product Right Sidebar</a></li>
<li><a href="product-left-sidebar.html">Product Left Sidebar</a></li>
<li><a href="product-3col.html">Product 3 Col</a></li>
<li><a href="{% url 'store:product' %}">Product 4 Col</a></li>
<li><a href="single-product.html">Single product</a></li>
</ul>
</li>
<!--
<li class="">
<ul class="list-unstyled">
<li><a href="user-dashboard.html">User Dashboard</a></li>
<li><a href="user-profile.html">User Profile</a></li>
<li><a href="address.html">Address</a></li>
<li><a href="all-order.html">All Order</a></li>
<li><a href="wishlist.html">Wishlist</a></li>
</ul>
</li>
-->
<li class="">
<ul class="list-unstyled">
<li><a href="{% url 'store:cart' 1 %}">Cart</a></li>
<li><a href="{% url 'store:checkout' %}">Checkout</a></li>
<li><a href="{% url 'success_view' %}">Confirmation</a></li>
<li><a href="{% url 'pricing' %}">Price table</a></li>
</ul>
</li>
<li class="">
<ul class="list-unstyled">
<li><a href="javascript:void(0)" class="px-md-0"><img class="img-responsive lazyestload" data-src="{% static 'img/home/pricing-1.jpg' %}" src="{% static 'img/home/pricing-1.jpg' %}" alt="logo"></a></li>
</ul>
</li>
</ul>
</li>
<li class="nav-item dropdown ">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" href="javascript:void(0)">Pages</a>