I'm reading "Beginning Django E-Commerce" but I stopped with template tags
the error message :
TemplateSyntaxError at /
'search_tags' is not a valid tag library: ImportError raised loading search.templatetags.search_tags: No module named models
The app tree looks like:
search/
-- __init__.py
-- models.py
templatetags/
-- __init__.py
-- search_tags.py
and search_tags.py
from django import template
from search.forms import SearchForm
import urllib
register = template.Library()
@register.inclusion_tag("tags/search_box.html")
def search_box(request):
q = request.GET.get('q', '')
form = SearchForm({'q': q })
return {'form': form }
@register.inclusion_tag('tags/pagination_links.html')
def pagination_links(request, paginator):
raw_params = request.GET.copy()
page = raw_params.get('page', 1)
p = paginator.page(page)
try:
del raw_params['page']
except KeyError:
pass
params = urllib.urlencode(raw_params)
return {'request': request,
'paginator': paginator,
'p': p,
'params': params }
Do you have any ideas ? thank you
search.py
from models import SearchTerm
from catalog.models import Product
from django.db.models import Q
STRIP_WORDS = ['a','an','and','by','for','from','in','no','not','of','on','or','that','the','to','with']
#store the search text in db
def store(request, q):
#if search term is three chars will store in db
if len(q) > 2:
term = SearchTerm()
term.q = q
term.ip_address = request.META.get('REMOTE_ADDR')
term.user = None
if request.user.is_authenticated():
term.user = requst.user
term.save()
# get pruduct matching
def products(search_text):
words = _prepate_words(search_text)
products = Product_words(search_text)
results = {}
results['products'] = []
# iterate through keywords
for word in words:
products = products.filter(Q(name__contains=word) | Q(description__icontains=word) | Q(sku__iexact=word) | Q(brand__icontains=word) | Q(meta_description__icontains=word) | Q(meta_keywords__icontains=word))
results['products'] = products
return results
#strip comon words , or limit to 5 words
def _prepate_words (search_text):
words = search_text.splite()
for common in STRIP_WORDS:
if common in words:
words.remove(common)
return words[0:5]
views.py
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.core.paginator import Paginator, InvalidPage, EmptyPage
import search
from ecomstore import settings
def results(request, template_name="search/results.html"):
# get current search phrase
q = request.GET.get('q', '')
# get current page number. Set to 1 is missing or invalid
try:
page = int(request.GET.get('page', 1))
except ValueError:
page = 1
# retrieve the matching products
matching = search.products(q).get('products')
# generate the pagintor object
paginator = Paginator(matching,
settings.PRODUCTS_PER_PAGE)
try:
results = paginator.page(page).object_list
except (InvalidPage, EmptyPage):
results = paginator.page(1).object_list
# store the search
search.store(request, q)
# the usual
page_title = 'Search Results for: ' + q
return render_to_response(template_name, locals(),
context_instance=RequestContext(request))