problem with get_absolute_url()

247 views
Skip to first unread message

Александр Мусаров

unread,
Aug 7, 2015, 2:15:25 PM8/7/15
to Django users
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 )

Александр Мусаров

unread,
Aug 7, 2015, 3:33:02 PM8/7/15
to Django users
 forgot to paste the model methods

@models.permalink
    def get_absolute_url(self):
        return ('catalog_category', (), {'category_slug' : self.category_slug })

@models.permalink
    def get_absolute_url(self):
        return ('catalog_product', {}, {'product_slug': self.product_slug})

пятница, 7 августа 2015 г., 21:15:25 UTC+3 пользователь Александр Мусаров написал:

James Schneider

unread,
Aug 7, 2015, 5:03:40 PM8/7/15
to django...@googlegroups.com

You don't have a URL named 'catalog_product'. Check your URL's, it looks like you are passing an arg and a kwarg to the second pattern.

-James

--
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...@googlegroups.com.
To post to this group, send email to django...@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/a9d529d0-57fb-418d-9d21-f92168e5a284%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Александр Мусаров

unread,
Aug 9, 2015, 12:40:39 PM8/9/15
to Django users
Changed the URLS as follows, doesn't seem to have helped much, still getting the same error..
    url(r'^(?P<category_slug>[-\w]+)/$', views.category, name = 'catalog_category' ),
    url(r'^(?P<category_slug>[-\w]+)/(?P<product_slug>[-\w]+)/$', views.product, name = 'catalog_product'),

суббота, 8 августа 2015 г., 0:03:40 UTC+3 пользователь James Schneider написал:

James Schneider

unread,
Aug 9, 2015, 1:03:06 PM8/9/15
to django...@googlegroups.com

For a 'catalog_product' URL, you need to provide two slug arguments, one for the category and one for the product. Your permalink function only provides a value for product_slug, which is why there is no match against the regex.

You should also rewrite those functions to remove the @permalink decorator as its use is discouraged and deprecated. Use reverse() in the function body instead.

-James

Александр Мусаров

unread,
Aug 10, 2015, 9:16:02 AM8/10/15
to Django users
THANX A LOT
Reply all
Reply to author
Forward
0 new messages