Friday, August 7, 2015

problem with get_absolute_url()

Hi, just wrapping my head around django, and trying to write an ecommerce store, when trying to pull out all products belonging to a category getting such an error

NoReverseMatch at /catalog/smartphony/

Reverse for 'catalog_product' with arguments '()' and keyword arguments '{'product_slug': 'samsung-galaxy-s5-sm-g900f-16gb'}' not found. 0 pattern(s) tried: []
Request Method: GET
Request URL: http://127.0.0.1:8000/catalog/smartphony/
Django Version: 1.8.2
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'catalog_product' with arguments '()' and keyword arguments '{'product_slug': 'samsung-galaxy-s5-sm-g900f-16gb'}' not found. 0 pattern(s) tried: []
Exception Location: C:\Users\LAPTOP\djangoenvs\py34\lib\site-packages\django\core\urlresolvers.py in _reverse_with_prefix, line 496
Python Executable: C:\Users\LAPTOP\djangoenvs\py34\Scripts\python.exe
Python Version: 3.4.3
Python Path:
['C:\\Users\\LAPTOP\\Desktop\\djangodev\\eshop',   'C:\\Users\\LAPTOP\\djangoenvs\\py34\\lib\\site-packages\\setuptools-18.0.1-py3.4.egg',   'C:\\Windows\\system32\\python34.zip',   'C:\\Users\\LAPTOP\\djangoenvs\\py34\\DLLs',   'C:\\Users\\LAPTOP\\djangoenvs\\py34\\lib',   'C:\\Users\\LAPTOP\\djangoenvs\\py34\\Scripts',   'C:\\Python34\\Lib',   'C:\\Python34\\DLLs',   'C:\\Users\\LAPTOP\\djangoenvs\\py34',   'C:\\Users\\LAPTOP\\djangoenvs\\py34\\lib\\site-packages']
Server time: Пт, 7 Авг 2015 21:07:01 +0300

Error during template rendering

In template C:\Users\LAPTOP\Desktop\djangodev\eshop\catalog\templates\categories\category.html, error at line 17

Reverse for 'catalog_product' with arguments '()' and keyword arguments '{'product_slug': 'samsung-galaxy-s5-sm-g900f-16gb'}' not found. 0 pattern(s) tried: []

7 <ol class="breadcrumb">
8 <li><a href="/">Главная</a></li>
9 <li><a href="/catalog/">Каталог</a></li>
10 <li class="active">{{ c.category_name }}</li>
11 </ol>
12 </div>
13 <h1>{{ c.category_name }}</h1>
14 <br/><br/>
15 {% for p in products %}
16 <div class = "product_thumbnail">


17 <a href = " {{ p.get_absolute_url }} ">-- Here the debugger shows the mistake


18
<img src = "{{ MEDIA_URL }}/catalog/products/thumbnails/{{ p.product_image }}" alt = "{{ p.product_name }}" class = "bn" />
19 <br/>
20 {{ p.product_name }}
21 </a>
22 </div>
23 {% endfor %}
24
25 {% endblock %}


Here are my URL patterns

url(r'^(?P<category_slug>[-\w]+)/$', views.category, name = 'category_detail' ),
 url(r'^(?P<category_slug>[-\w]+)/(?P<product_slug>[-\w]+)/$', views.product, 'catalog_product', name = 'product_detail'),

Here are the corresponding views

def category(request, category_slug):
    category_list = Category.objects.filter(category_is_active = True)
    c = get_object_or_404(Category, category_slug = category_slug)
    products = c.product_set.all()
    page_title = c.category_name
    meta_keywords = c.category_meta_keywords
    meta_description = c.category_meta_description
    return render(request, 'categories/category.html', locals())

def product(request, product_slug):
    category_list = Category.objects.filter(category_is_active = True)
    p = get_object_or_404(Product, product_slug = product_slug)
    categories = p.product_categories.all()
    page_title = p.product_name
    meta_keywords = p.product_meta_keywords
    meta_description = p.product_meta_description
   
    # need to evaluate the HTTP method
    if request.method == 'POST':
        # add to cart... create the bound form
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        # check if posted data is valid
        if form.is_valid():
            # add to cart and redirect to cart page
            cart.add_to_cart(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url = urlresolvers.reverse('show_cart')
            return HttpResponseRedirect(url)
    else:
        # it's a GET, create the unbound form. Note request as a kwarg
        form = ProductAddToCartForm(request = request, label_suffix = ':')
    # assign the hidden input the product slug
    form.fields['product_slug'].widget.attrs['value'] = product_slug
    # set the test cookie on our first GET request
    request.session.set_test_cookie()
    return render(request, 'products/product.html', locals())

and here is the part of the corresponding view

{% for p in products %}       
      <div class = "product_thumbnail">
        <a href = "{{ p.get_absolute_url }}">
          <img src = "{{ MEDIA_URL }}/catalog/products/thumbnails/{{ p.product_image }}" alt = "{{ p.product_name }}" class = "bn" />
          <br/>
            {{ p.product_name }}
        </a>
        </div>
    {% endfor %}      

Thanx in advance )

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/6d1e58e4-6f6a-4ca5-b2e9-77a7c78cba3e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment