I am having trouble understanding code of website in tutorial. what i understood is that urls.py take a argument as product_slug which is used to get object in my show_product view and another argument is used in as template_name but what is argument 'catalog_product' doing it is defined in my model.when I remove 'catalog_product' I get error as:
no reverse match at /category/guitar
please explain how does this argument get_absolute_url work and how and where I can use it?
model.py is:
def get_absolute_url(self):
return reverse('catalog_product', (), { 'product_slug': self.slug })
I am having uurls.py as:
url(r'^product/(?P<product_slug>[-\w]+)/$',show_product, {'template_name':'catalog/product.html'}, 'catalog_product')
and views.py:
def show_product(request, product_slug, template_name="catalog/product.html"):
p = get_object_or_404(Product, slug=product_slug)
categories = p.categories.filter(is_active=True)
page_title = p.name
meta_keywords = p.meta_keywords
meta_description = p.meta_description
return render(request, template_name, locals())
template product.html :-
% extends "catalog.html" %}
{% block content %}
<div class="product_image" >
{% load static %}
<img src="{% static 'images/products/main/{{ p.image}}' %}" alt="{{ p.name }}" />
</div>
<h1>{{ p.name }}</h1>
Brand: <em>{{ p.brand }}</em>
<br /><br />
SKU: {{ p.sku }}
<br />
In categor{{ categories.count|pluralize:"y,ies" }}:
{% for c in categories %}
<a href="{{ c.get_absolute_url }}">{{ c.name }}</a>
{% if not forloop.last %}, {% endif %}
{% endfor %}
<br /><br />
{% if p.sale_price %}
Was: <del>$ {{ p.old_price }}</del>
<br />
Now: $ {{ p.price }}
{% else %}
Price: $ {{ p.price }}
{% endif %}
<br /><br />
[add to cart button]
<br /><br />
<div class="cb"><br /><br /></div>
<h3>Product Description</h3>
{{ p.description }}
{% endblock %}