其他模板未在我的基本模板中继承 FOR LOOP
Posted
技术标签:
【中文标题】其他模板未在我的基本模板中继承 FOR LOOP【英文标题】:Other templates are not inheriting FOR LOOP in my base template 【发布时间】:2017-07-08 07:49:27 【问题描述】:循环实际上是用于导航。列出类别和在每个类别下销售或提供服务的商店。 请..我是 Django 和 Python 的新手。如果我得到这些东西的速度很慢,我很抱歉......谢谢
models.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)
views.py
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)
base.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>
index.html
% extends "base.html" %
% block title % This is Homepage % endblock title %
网址.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.homepage),
url(r'^base/', views.basefile)
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root= settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, docuemt_root= settings.MEDIA_ROOT)
它在 base.html 中运行良好。但是当我在索引或其他模板中 % extends base.html % 时。模板加载得很好,但 FOR 循环不起作用。我到底做错了什么?
【问题讨论】:
您在后端实现shop_name1
的上下文?
@SancaKembang 我真的不明白你的意思。
对于 shop_name1 中的 shop_name1?有趣的循环
@AlexanderTyapkov .. 我只是 django 的初学者。我真的只是在学习很多新东西。我最近刚刚更改了您上面提到的变量名称.. 谢谢!!!... 请您帮我处理 FOR 循环...
@DanielKilanko 好的,只需提供更多代码,例如正在使用的视图以及模板的完整版本
【参考方案1】:
您应该像在基本文件视图中一样在主页视图中提供上下文:
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)
【讨论】:
以上是关于其他模板未在我的基本模板中继承 FOR LOOP的主要内容,如果未能解决你的问题,请参考以下文章