Added views for part and part category

- Worked out simple linking
- Category path displaying correctly
- Category page lists parts in category
This commit is contained in:
Oliver Walters 2017-03-26 10:06:00 +11:00
parent ffb9132de2
commit ddd927c436
5 changed files with 56 additions and 3 deletions

View File

@ -9,10 +9,20 @@ class PartCategory(models.Model):
def __str__(self):
if self.parent:
return str(self.parent) + "/" + self.name
return "/".join([p.name for p in self.path]) + "/" + self.name
else:
return self.name
# Return the parent path of this category
@property
def path(self):
parent_path = []
if self.parent:
parent_path = self.parent.path + [self.parent]
return parent_path
class Part(models.Model):
name = models.CharField(max_length=100)
description = models.CharField(max_length=250, blank=True)
@ -25,4 +35,7 @@ class Part(models.Model):
ipn = self.IPN,
name = self.name)
else:
return self.name
return self.name

View File

@ -0,0 +1,17 @@
<h1>Category: {{ category.name }}</h1>
<h2>Path: {{ category }}</h2>
<br>
Path<br>
{# Construct the category path #}
{% for path_item in category.path %}
<a href="../{{path_item.pk}}">{{path_item.name}}</a>/
{% endfor %}
<br>Parts:<br>
{% for part in category.part_set.all %}
<a href="/part/{{part.pk}}">{{part.name}}</a><br>
{% endfor %}

View File

@ -0,0 +1 @@
{{part}}

View File

@ -3,5 +3,10 @@ from django.conf.urls import url
from . import views
urlpatterns = [
# Display part detail
url(r'^(?P<part_id>[0-9]+)/$', views.partdetail, name='detail'),
# Display a part category
url(r'^category/(?P<category_id>[0-9]+)/$', views.category, name='category'),
url(r'^$', views.index, name='index')
]

View File

@ -1,5 +1,22 @@
from django.shortcuts import render
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from .models import PartCategory, Part
def index(request):
return HttpResponse("Hello world. This is the parts page")
def partdetail(request, part_id):
part = get_object_or_404(Part, pk=part_id)
return render(request, 'part/detail.html',
{'part': part})
def category(request, category_id):
# Find the category
cat = get_object_or_404(PartCategory, pk=category_id)
return render(request, 'part/category.html',
{'category': cat})