Engin
Engin

Reputation: 18

two views in one page django

I am new on django and got a problem. I got 2 apps, one is slider which i get slider images and other one is products. I've featured product option in my product model which i want to show on index page. This is my views.py

def index(request):
allslides = SliderImage.objects.all()

context = {
    'allslides': allslides
}
return render(request, 'pages/index.html', context)

def fproducts(request):
    fproducts = Product.objects.filter(is_featured=True)
    context = {
        'fproducts': fproducts
    }
    return render(request, 'pages/index.html', context)

I've made some research and it looks django doesn't allow 2 views in one page. That's my index.html

<div id="homepageslider" class="flexslider">
<ul class="slides">
    {% for s in  allslides %}
<li class=""><img src="{{ s.image.url }}" title="{{ s.alt }}"></li>
{% endfor %}
</ul>
</div>
<div class="container">
{% if fproducts %}
    {% for product in fproducts %}
        <div class="col-md-4 col-lg-3 col-sm-6 col-12 mb-4 px-1">
            <div class="card">
             <a href="{% url 'productdetail' product.id %}"><img src="{{ product.main_image.url }}" class="card-img-top" alt="..."></a>
              <div class="card-body">
                <a href="{% url 'productdetail' product.id %}"><h5 class="card-title text-truncate">{{ product.title }}</h5></a>
                  <a href="{% url 'productdetail' product.id %}"><p class="card-text">{{ product.kod }}</p></a>
              </div>
            </div>
        </div>
    {% endfor %}
    {% else %}
    <p>No Products</p>
{% endif %}

Thanks for help.

Upvotes: 0

Views: 139

Answers (1)

Sergey Pugach
Sergey Pugach

Reputation: 5669

Do it in one view:

def index(request):
    allslides = SliderImage.objects.all()
    fproducts = Product.objects.filter(is_featured=True)
    context = {
        'allslides': allslides,
        'fproducts': fproducts,
    }
    return render(request, 'pages/index.html', context)

Upvotes: 2

Related Questions