Chris Tyerman
Chris Tyerman

Reputation: 11

Reverse for 'product' with no arguments not found. 1 pattern(s) tried: [u'products/(?P<product>[-\\w]+)/$']

I'm trying to link to a page in the Django admin; I'm using the slugField to do so. I have created a base URL in the main 'urls.py' file which includes a namespace. I then have declared my product urls in the urls.py file, which directs to my template and view.

index.html

{% for item in products %}
<div class="col-sm-12 col-md-4 col-lg-4">
  <div class="box">
    {% if item.image_one %}
    <div class="image">
      <img src="{{ item.image_one.url }}" alt="{{ item.name }}">
    </div>
    {% endif %}
    <div class="content">
      <h4>{{ item.name }}</h4>
      <a href="{% url 'products:product' item.slug %}" class="button">View Product</a>
    </div>
  </div>
</div>
{% endfor %}

urls.py

urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^(?P<product>[-\w]+)/$', views.product, name='product'),]

views.py

def product(request, product):

    product = get_object_or_404(Product, slug=product)

    return render(request, 'products/product.html',{
        'product':product,
})


 def index(request):

     products = Product.objects.all()

     return render(request, 'products/index.html',{
         'products': products,
})

rendered HTML on index.html page:

<section id="products">
  <div class="container">
    <div class="row">   
      <div class="col-sm-12 col-md-4 col-lg-4">
        <div class="box">
          <div class="image">
            <img src="/media/temp-image.png" alt="product name">
          </div>
          <div class="content">
            <h4>product name</h4>
            <a href="/products/product-name/" class="button">View Product</a>
          </div>
        </div>
      </div>

      <div class="col-sm-12 col-md-4 col-lg-4">
        <div class="box">
          <div class="image">
            <img src="/media/temp-image_uQcyyVv.png" alt="Another product">
          </div>
          <div class="content">
            <h4>Another product</h4>
            <a href="/products/another-product/" class="button">View Product</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</section>

Upvotes: 1

Views: 164

Answers (2)

Chris Tyerman
Chris Tyerman

Reputation: 11

Thank you for helping. Turns out I had referenced the URL string without the slug perimeter elsewhere in the document I was trying to open and therefore it was causing an error.

Appreciate the help and support

Upvotes: 0

Brian Dant
Brian Dant

Reputation: 4149

It's not clear why the url reverse fails, but you should not do what you are doing with the admin. Instead, change your urls.py to not mimic the admin:

url(r'^(?P<product>[-\w]+)/$', views.product, name='my-product-view')

Then, in your template:

<a href="{% url 'my-product-view' item.slug %}" class="button">View Product</a>

If that doesn't work, ensure that you can reverse by hand. Get a Django shell:

$ python manage.py shell

Then try to reverse the url with the id from one of your objects:

>>> from django.urlresolver import reverse
>>> aproduct = Product.objects.first()
>>> reverse('my-product-view', args=(aproduct.slug,))

What I do above in the shell is more or less what happens behind the scenes with your {% url %} tag, so this will give us some insight into where the problem lies. If that doesn't work, you've got a problem with your settings. Confirm that your INSTALLED_APPS contains the app in which this urls.py lives.

Finally, the error you see would seem to indicate that you're missing a slug on one of your products ("reverse for product with no arguments ..."). Confirm that this is not the case by iterating in the shell over your products:

>>> for p in Products.objects.all():
>>>     if not p.slug: 
>>>         print(p)

Upvotes: 1

Related Questions