I have a website for buying/selling auto parts running on Python Django framework. And I have a navigation bar which shows how many auto parts I have in stock (e.x Audi (1), BMW(23)...). When I add new item in my database(mysql) navigation bar automaticly recounts all items. If there are no title in the list by auto maker it creates one.
So my question is, how could I do this navigation bar clickable(I press on Audi and I get redirected to new page where all audi parts are filtered). I need to create URLs pattern and View for that. I have no clue how to generate these URLs.
Here is a views.py:
def sell_view(request):
entries = Parduodami.objects.all()
modeliai = []
for item in entries: #if there are no brand in a list, it adds it
if item.brand not in modeliai:
modeliai.append(item.brand)
modeliai.sort()
modeliai_kiekis = []
for item in modeliai:
modeliai_kiekis.append(Parduodami.objects.filter(brand=str(item)).count()) #counts how many items by that brand is there
ctx = {'entries':entries, 'modeliai': zip(modeliai_kiekis, modeliai)}
return render(request, 'parduodami.html', ctx)
Models.py:
class Parduodami(models.Model):
brand = models.CharField(max_length=30)
name = models.CharField(max_length=40)
model_year = models.CharField(max_length=20)
engine_code = models.CharField(max_length=20, blank=True, null=True)
engine_type = models.CharField(max_length=20, blank=True, null=True)
cc = models.CharField(max_length=10)
power = models.CharField(max_length=20, blank=True, null=True)
transmition_type = models.CharField(max_length=20)
selling_part = models.TextField()
published = models.BooleanField()
def __unicode__(self):
return self.name
Sorry if my question is hard to understand.
Thanks in advance!