Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/91.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 显示N-父N-子菜单Django_Mysql_Html_Python 3.x_Django Templates_Django Views - Fatal编程技术网

Mysql 显示N-父N-子菜单Django

Mysql 显示N-父N-子菜单Django,mysql,html,python-3.x,django-templates,django-views,Mysql,Html,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

在这里,我用一段代码结巴了

让我解释一下我想要什么 这样显示我的项目

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)