显示N - 父N - 子菜单Django

时间:2014-06-27 06:46:28

标签: mysql html5 python-3.x django-templates django-views

在这里我坚持了一段代码

让我解释一下我想要的东西  显示我的项目

Chocolate
    Hot chocolate
    White Chocolate
        Salted caramel hot chocolate
    Peppermint hot chocolate
Espresso Beverage
    caffe Americano
    caffe latte
       caffe Moche
    Cappuccino
Frappuccino
    a
       b
          c
              d

这里我使用Django视图和模板来显示

在我的Mysql表中,我已经插入了所有这些记录 四栏1.id 2.name 3.slug 4.parent

在上面的显示中

 eg  Chocolate  is with id one  and parent 0
       Hot chocolate  is with id 2 and parent 1
       White Chocolate  is with id 3 and parent 1
       Salted caramel hot chocolate is with id 4 and parent 3
       Espresso Beverage  is with id 6 and parent 0

希望你明白

我的HTML代码

 {% for cat in user %}
  {% if cat.parent = 0%}
  <ul id="listMenu" style="list-style-type:none; margin-left:-50">  

      <li class="listparent"><p style="font-size:15px;"><a href="#" style="text-decoration: none;color: black;">{{cat.name}}<br></a></p></li>
     {% for cat1 in user %}
    {% if cat1.parent = cat.id%}
     <ul class="menuitems" style="list-style-type:none;">  

    <li class="listchild"> <p class="Tier1" style="font-size:15px;"><a href="#" style="text-decoration: none;color: black;" >{{cat1.name}}<br></a></p></li>

      </ul>

     {%endif%}
       {% endfor %} 
       </ul>
      {%endif%}
       {% endfor %}

和我的观点

error = 50
@login_required 
def categories(request):
     context = RequestContext(request)
     user = Categories.objects.all()
     if request.method == 'POST':
           catform = CategoryForm(data=request.POST)
           post_values = request.POST.copy() 
           post_values['userid'] = request.user.id
           post_values['createdby'] = request.user.id
           post_values['modifiedby'] = request.user.id
           catform = CategoryForm(post_values)        
           if catform.is_valid():
                 is_valid = True 
             profile = catform.save(commit=False)
                 profile.save()
                 registered = True
                 messages.add_message(request, error, 'Category Added.')  
           else:
                 messages.add_message(request, messages.INFO, 'Invalid input check your slug or order.')
     else:
         catform = CategoryForm()
     paginator = Paginator(user, 20)     
     try: page = int(request.GET.get("page", '1'))
     except ValueError: page = 1

     try:
         user = paginator.page(page)
     except (InvalidPage, EmptyPage):
         user = paginator.page(paginator.num_pages)
     return render_to_response('myapp/categories.html',{'catform':catform,'user':user},context)

请建议我获得所需输出的逻辑

使用上面的代码我可以实现1个父1孩子 不是父母的孩子

提前致谢:_)

0 个答案:

没有答案