TemplateDoesNotExist at /product/1/ productDetail.html
I am making an about page for every item. There is a problem that occurred when I am pressing 'View' on my store page: TemplateDoesNotExist at /product/1/ productDetail.html
I have tried specifying the directory by putting {{% extends store/main.html %}}, it did not help. Please assist me.
store/views.py
from django.shortcuts import render, redirect
from django.http import JsonResponse
import json
import datetime
from django.urls import reverse
from .forms import CommentForm
from .models import *
from .utils import cookieCart, cartData, guestOrder
def store(request):
data = cartData(request)
cartItems = data['cartItems']
order = data['order']
items = data['items']
products = Product.objects.all()
context = {'products': products, 'cartItems': cartItems}
return render(request, 'store/store.html', context)
def cart(request):
data = cartData(request)
cartItems = data['cartItems']
order = data['order']
items = data['items']
context = {'items': items, 'order': order, 'cartItems': cartItems}
return render(request, 'store/cart.html', context)
def checkout(request):
data = cartData(request)
cartItems = data['cartItems']
order = data['order']
items = data['items']
context = {'items': items, 'order': order, 'cartItems': cartItems}
return render(request, 'store/checkout.html', context)
def updateItem(request):
data = json.loads(request.body)
productId = data['productId']
action = data['action']
print('Action:', action)
print('Product:', productId)
customer = request.user.customer
product = Product.objects.get(id=productId)
order, created = Order.objects.get_or_create(customer=customer, complete=False)
orderItem, created = OrderItem.objects.get_or_create(order=order, product=product)
if action == 'add':
orderItem.quantity = (orderItem.quantity + 1)
elif action == 'remove':
orderItem.quantity = (orderItem.quantity - 1)
orderItem.save()
if orderItem.quantity <= 0:
orderItem.delete()
return JsonResponse('Item was added', safe=False)
def processOrder(request):
transaction_id = datetime.datetime.now().timestamp()
data = json.loads(request.body)
if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
else:
customer, order = guestOrder(request, data)
total = float(data['form']['total'])
order.transaction_id = transaction_id
if total == order.get_cart_total:
order.complete = True
order.save()
if order.shipping == True:
ShippingAddress.objects.create(
customer=customer,
order=order,
address=data['shipping']['address'],
city=data['shipping']['city'],
state=data['shipping']['state'],
zipcode=data['shipping']['zipcode'],
)
return JsonResponse('Payment submitted..', safe=False)
def productDetail(request, pk):
eachProduct = Product.objects.get(id=pk)
num_comments = Comment.objects.filter(product=eachProduct).count()
context = {
'eachProduct': eachProduct,
'num_comments': num_comments,
}
return render(request, 'productDetail.html', context)
def add_comment(request, pk):
eachProduct = Product.objects.get(id=pk)
form = CommentForm(instance=eachProduct)
if request.method == 'POST':
form = CommentForm(request.POST, instance=eachProduct)
if form.is_valid():
name = request.user.username
body = form.cleaned_data['comment_body']
c = Comment(product=eachProduct, commenter_name=name, comment_body=body, date_added=datetime.now())
c.save()
return redirect('showProducts')
else:
print('form is invalid')
else:
form = CommentForm()
context = {
'form': form
}
return render(request, 'add_comment.html', context)
def delete_comment(request, pk):
comment = Comment.objects.filter(product=pk).last()
product_id = comment.product.id
comment.delete()
return redirect(reverse('product', args=[product_id]))
sotre/urls.py
from django.urls import path
from . import views
urlpatterns = [
# Leave as empty string for base url
path('', views.store, name="store"),
path('cart/', views.cart, name="cart"),
path('checkout/', views.checkout, name="checkout"),
path('update_item/', views.updateItem, name="update_item"),
path('process_order/', views.processOrder, name="process_order"),
path('product/<int:pk>/', views.productDetail, name='product'),
path('product/<int:pk>/add-comment', views.add_comment, name='add-comment'),
path('product/<int:pk>/delete-comment', views.delete_comment, name='delete-comment'),
]
why am I getting this error, if productDetail exists?
Your template path is incorrect, since your template is inside the store directory you have to specify a relative path to templates.
Change this
return render(request, 'productDetail.html', context)
to
return render(request, 'store/productDetail.html', context)