1

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

foto

Sorry if my question is hard to understand.

Thanks in advance!

0

1 Answer 1

1

You could make a url like this:

url(r'/brand/(?P<brand_name>\w', views.brand_display, name='brand_display')

Then in the view, you can access the brand_name that was entered in the url:

def brand_display(request, brand_name):
    # then load the brand by name: 
    test = Parduodami.objects.get(brand__iexact=brand_name)

then to print the url in your template like this:

{% url brand_display 'audi' %}

Or manually: (use {% url %}!)

/brand/audi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.