我使用循环在导航栏上查看我的类别。导航栏包含在所有模板中的布局和布局中。我在导航栏中放置循环以显示类别。如果我将我的上下文放到索引定义中 它可以工作,但是如何在所有页面上显示我的循环?我想我需要全局定义。
我的是 Django 2.1.2,Python 3.5
视图.py:
def index(request):
products = Product.objects.filter()
catloops = Category.objects.filter()
context = {
"products":products,
"catloops":catloops,
}
return render(request,"index.html", context)
def cat_nav(request):
catloops = Category.objects.filter()
context = {
"catloops":catloops,
}
return render(render, "????", context)
网址.py:
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.index, name="index"),
path('about/',views.about, name="about"),
path('products/',include("product.urls")),
path('user/',include("user.urls")),
path('products/',views.products, name="products"),
path('myorders/',views.myorders, name="myorders"),
path('dashboard/',views.dashboard, name="dashboard"),
path('myprofile/',views.myprofile, name="myprofile"),
path('????',views.cat_nav, name="cat_nav"),
#user/register.. geldiğinde git user app'inden al
]
导航栏.html:
<li class="nav-item dropdown menu-large"><a href="#" data-toggle="dropdown" class="dropdown-toggle">Categories <b class="caret"></b></a>
<ul class="dropdown-menu megamenu">
<li>
<div class="row">
<div class="col-lg-6"><img src="{% static 'img/template-homepage.png' %}" alt="" class="img-fluid d-none d-lg-block"></div>
<div class="col-lg-3 col-md-6">
<h5>Used Products</h5>
<ul class="list-unstyled mb-3">
{% for category in catloops %}
<li class="dropdown-item"><a href="/products" class="nav-link">{{category.category_name}}</a></li>
{% endfor %}
如果我将我的上下文放到索引定义中,它就可以工作,但是当我转到其他页面(网址)时,我看不到我的类别。
相关分类