其他模板不在我的基本模板中继承FOR LOOP

时间:2017-02-18 21:10:54

标签: django python-2.7 django-models django-templates django-views

循环实际上用于导航。列出在每个类别下销售或提供服务的类别和商店。 请..我是Django和Python的新手。如果我得到这些东西很慢,我很抱歉...谢谢

models.py

t( (t['A']='US',t['B']='CA',t['C']='SFO') , 'y') = "IT"
t( (t['A']='US',t['B']='CA',t['C']='LA' ) , '1') = 255

views.py

class ShopCategories(models.Model):
category = models.CharField(max_length=50, unique=True)
def __str__(self):
    return self.category

class NewShop(models.Model):
    category = models.ForeignKey(ShopCategories)
    main_image = models.FileField(null=True, blank=True)
    name = models.CharField(max_length=100, unique=True)
    tagline = models.CharField(max_length=50, default='Enter tagline here2')
    description = models.TextField(default='enter shop description')
    shop_image = models.FileField(null=True, blank=True)

base.html

def homepage(request):
   return render_to_response('index.html')

def basefile(request):
    cat1 = NewShop.objects.filter(category_id=1)
    cat2 = NewShop.objects.filter(category_id=2)
    cat3 = NewShop.objects.filter(category_id=3)
    cat4 = NewShop.objects.filter(category_id=4)
    name1 = ShopCategories.objects.filter(id=1)
    name2 = ShopCategories.objects.filter(id=2)
    name3 = ShopCategories.objects.filter(id=3)
    name4 = ShopCategories.objects.filter(id=4)

    return  render_to_response('base.html', {'Shop_cat1':cat1, 'Shop_cat2':cat2, 'Shop_cat3':cat3,
                                        'Shop_cat4':cat4,'shop_name1':name1, 'shop_name2':name2,
                                        'shop_name3':name3, 'shop_name4':name4})

index.html

<ul class="nav navbar-nav">
  <li class="current-menu-item">
     <ul class="nav navbar-nav">
        <li class="current-menu-item">
           <a href="/">ENTOURAGE MALL</a>
        </li>
        <li class="dropdown pi-mega-fw menu-item-has-children">
           <a href="#" data-toggle="dropdown" class="dropdown-toggle" role="button">SHOPS</a>
     <ul class="dropdown-menu">
        <li>
           <div class="pi-mega-content">
              <div class="row">
                 <div class="col-md-3 pi-mm-col">
                    <ul class="pi-mm-list">
                    {% for shop in shop_name1 %}
                       <li>
                          <h3> {{ shop }}</h3>
                      </li>
                    {% endfor %}
                    {% for cat in Shop_cat1  %}
                       <li><a href="#">{{ cat }}</a></li>
                    {% endfor %}
                   </ul>
                </div>
                <div class="col-md-3 pi-mm-col">
                    <ul class="pi-mm-list">
                    {% for shop in shop_name2 %}
                       <li>
                          <h3> {{ shop }}</h3>
                      </li>
                    {% endfor %}
                    {% for cat in Shop_cat2  %}
                       <li><a href="#">{{ cat }}</a></li>
                    {% endfor %}
                   </ul>
                </div>
                <div class="col-md-3 pi-mm-col">
                    <ul class="pi-mm-list">
                    {% for shop in shop_name3 %}
                       <li>
                          <h3> {{ shop }}</h3>
                      </li>
                    {% endfor %}
                    {% for cat in Shop_cat3  %}
                       <li><a href="#">{{ cat }}</a></li>
                    {% endfor %}
                   </ul>
                </div>
             </div>
          </div>
       </li>
    </ul>
 </li>
 <li class="menu-item">
    <a href="contact" role="button">ENTOURAGE LOUNGE</a>
 </li>
 <li class="menu-item">
    <a href="contact" role="button">About MALL</a>
 </li>
 <li class="menu-item">
    <a href="contact" role="button">BLOG</a>
 </li>
</ul>

Urls.py

{% extends "base.html" %}

{% block title %} This is Homepage {% endblock title %}

它在base.html中运行得很好。但是当我在索引或其他模板中{%extends base.html%}时。模板加载得很好,但FOR循环不起作用。我究竟做错了什么?

1 个答案:

答案 0 :(得分:0)

您应该以与在基本文件视图中相同的方式在主页视图中提供上下文:

def homepage(request):
    cat1 = NewShop.objects.filter(category_id=1)
    cat2 = NewShop.objects.filter(category_id=2)
    cat3 = NewShop.objects.filter(category_id=3)
    cat4 = NewShop.objects.filter(category_id=4)
    name1 = ShopCategories.objects.filter(id=1)
    name2 = ShopCategories.objects.filter(id=2)
    name3 = ShopCategories.objects.filter(id=3)
    name4 = ShopCategories.objects.filter(id=4)

    return render_to_response('index.html', {'Shop_cat1':cat1, 'Shop_cat2':cat2, 'Shop_cat3':cat3,
                                        'Shop_cat4':cat4,'shop_name1':name1, 'shop_name2':name2,
                                        'shop_name3':name3, 'shop_name4':name4})