Skip to content

Commit 82a91a3

Browse files
committed
linkando o caminho dos produtos as categorias
1 parent 58ecd04 commit 82a91a3

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

catalog/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.db import models
2+
from django.core.urlresolvers import reverse
23

34
class Category(models.Model):
45

@@ -16,6 +17,9 @@ class Meta:
1617
def __str__(self):
1718
return self.name
1819

20+
def get_absolut_url(self):
21+
return reverse('catalog:category', kwargs = {'slug': self.slug})
22+
1923
class Product(models.Model):
2024

2125
name = models.CharField('Nome', max_length=100)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{% extends "base.html" %}
2+
3+
{% block title %}
4+
{{ current_category }} | {{ block.super }}
5+
{% endblock %}
6+
7+
{% block container %}
8+
<div class="page-header">
9+
<h1>{{ current_category }}</h1>
10+
</div>
11+
<div class="row">
12+
{% for product in product_list %}
13+
<div class="col-sm-6 col-md-4">
14+
<div class="thumbnail">
15+
<a href="#">
16+
<img src="http://placehold.it/350x250" alt="" />
17+
</a>
18+
<div class="caption">
19+
<h3>{{ product }}</h3>
20+
{{ product.description|linebreaks }}
21+
<p><a href="#" class="btn btn-success" role="button">Button</a> <a href="#" class="btn btn-default" role="button">Button</a></p>
22+
</div>
23+
</div>
24+
</div>
25+
{% endfor %}
26+
</div>
27+
{% endblock %}

catalog/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
urlpatterns = [
66
url(r'^$', views.product_list, name = 'product_list'),
7+
url(r'^(?P<slug>[\w_-]+)$', views.category, name = 'category'),
78
]

catalog/views.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
from django.shortcuts import render
22

3-
from .models import Product
3+
from .models import Product, Category
44

55
def product_list(request):
66
context = {
77
'product_list': Product.objects.all()
88
}
9-
return render(request, 'catalog/product_list.html',context)
9+
return render(request, 'catalog/product_list.html',context)
10+
11+
def category(request, slug):
12+
category = Category.objects.get(slug = slug)
13+
context = {
14+
'current_category': category,
15+
'product_list': Product.objects.filter(category = category),
16+
}
17+
return render(request, 'catalog/category.html',context)

core/templates/base.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<a class="dropdown-toggle" data-toggle="dropdown" href="{% url 'catalog:product_list' %}" id="download">Categorias <span class="caret"></span></a>
3030
<ul class="dropdown-menu" aria-labelledby="download">
3131
{% for category in categories %}
32-
<li><a href="#">{{category}}</a></li>
32+
<li><a href="{{ category.get_absolut_url }}">{{category}}</a></li>
3333
{% endfor%}
3434
</ul>
3535
</li>

0 commit comments

Comments
 (0)