Reputation: 11
I'm getting this error Failed lookup for key [category]
in [{'True': True, 'False': False, 'None': None}, {}, {}, ....]
in my application I have category and subcategory which repeated too much in my view for every function and for child or subcategory I used pip install django-mptt
but it was Ok and worked will. So I decided to use custom template tags instead but right now I'm facing with this error
For more info you can take a look to my code. myapptags.py
from django import template
from django.db.models import Sum
from django.urls import reverse
from mysite import settings
from order.models import ShopCard
from product.models import Category
register = template.Library()
@register.simple_tag
def categorylist():
return Category.objects.all()
code for views.py
import json
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from django.urls import reverse
from django.shortcuts import render, redirect
from django.contrib import messages
from django.template.loader import render_to_string
from . models import Settings, ContactMessage, FAQ
from product.models import Category, Comment, Images, Product, Variants
from . forms import ContactForm, SearchForm
from product.forms import CommentForm
def index(request):
setting = Settings.objects.get(pk=1)
# category = Category.objects.all()
products_slider = Product.objects.all().order_by('id')[:4] #first 4 product
products_latest = Product.objects.all().order_by('-id')[:4] # latest
products_picked = Product.objects.all().order_by('?')[:4] # random
page = "index"
context = {
'setting': setting,
'page': page,
# 'category': category,
'products_slider': products_slider,
'products_latest': products_latest,
'products_picked': products_picked,
}
return render(request,'index.html',context)
side bar which contain my category and it's children
{% load myapptags %}
{% categorylist as category %}
{% load mptt_tags %}
<ul class="category-list">
{% recursetree category %}
<li class="dropdown side-dropdown">
<a href="{% url 'home:category-product' node.id node.slug %}" class="dropdown-toggle" {% if not node.is_leaf_node %} data-toggle="dropdown" aria-expanded="true" {% endif %} >
{{ node.title }} {% if not node.is_leaf_node %} <i class="fa fa-angle-right"> {% endif %}</i>
</a>
<div class="custom-menu">
<div class="row">
<div class="col-md-4">
{% if not node.is_leaf_node %}
<ul class="list-links">
<li>
<!-- <h3 class="list-links-title">Sub Categories</h3></li> -->
<hr>
<li><a href="#">{{ children }}</a></li>
</ul>
{% endif %}
<hr class="hidden-md hidden-lg">
</div>
</div>
<!-- <div class="row hidden-sm hidden-xs">
<div class="col-md-12">
<hr>
<a class="banner banner-1" href="#">
<img src="{{ node.image.url }}" style="{ width: 50%; height: 100px;}" alt="">
<div class="banner-caption text-center">
<h2 class="white-color">NEW COLLECTION</h2>
<h3 class="white-color font-weak">HOT DEAL</h3>
</div>
</a>
</div>
</div> -->
</div>
</li>
{% endrecursetree %}
</ul>
Thanks in advance for your nice answers
Upvotes: 0
Views: 1248
Reputation: 1
Edit your views.py
def index(request):
setting = Settings.objects.get(pk=1)
category = Category.objects.all()
products_slider = Product.objects.all().order_by('id')[:4] #first 4 product
products_latest = Product.objects.all().order_by('-id')[:4] # latest
products_picked = Product.objects.all().order_by('?')[:4] # random
page = "index"
context = {
'setting': setting,
'category': category,
'page': page,
'category': category,
'products_slider': products_slider,
'products_latest': products_latest,
'products_picked': products_picked,
}
return render(request,'index.html',context)
Upvotes: 0