1

How can I filter my table to only show the quotes from a project.

In order to display all quotes, I am using {% for quote in quotes.all %}

Now, I would like to display quote relative to a site. Which means when selecting my site_id, I`ll be able to only see the quote relative to this site_id. I could write something like

{% for quote in quotes.all %}
    {% if quote chantier.id %}
    {% endif %}
{% endfor %}

but this is wrong.

Here is my model for quote:

models.py

class Quote(models.Model):
    name = models.CharField(max_length=30)    
    site = models.ForeignKey(Site, on_delete=models.CASCADE)

How can I display all quotes from this site?

Many Thanks,

1
  • Where does this chantier comes from? Commented Jun 7, 2020 at 20:27

1 Answer 1

2

You can make a url path that contains the site_id:

# app_name/urls.py

from django.urls import path
from app_name import views

urlpatterns = [
    path('site/<int:site_id>/', views.quotes_of_site, name='quotes_of_site'),
    # …
]

in the view, you can then filter by site_id:

# app_name/views.py

from django.shortcuts import render
from app_name.models import Quote

def quotes_of_site(request, site_id):
    quotes = Quote.objects.filter(site_id=site_id)
    return render(request, 'name_of_template.html', {'quotes': quotes})

and then iterate over this QuerySet:

{% for quote in quotes %}
    …
{% endfor %}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.