我已经设置了Django mptt树,该树可以正确显示树。嵌套树由2种类型的对象(代理商和客户-在模型中添加)组成。在到达节点末尾的最终客户菜单之前,可以嵌套转销商菜单。根据类型,它们都应在单击时呈现一个html模板。但是,只有末端节点(叶节点)才能在单击时呈现html。而根节点或中间节点不响应关联的href菜单。我被困了将近2天,无法解决。请帮忙。
Django模型:
from django.db import models
from django.contrib.auth.models import User
from mptt.models import MPTTModel, TreeForeignKey
class Reseller(MPTTModel):
reseller_name = models.CharField(max_length=40)
reseller_email = models.EmailField(max_length=70,blank=True)
parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
class MPTTMeta:
order_insertion_by = ['reseller_name']
def __str__(self):
return self.reseller_name
class Customer(models.Model):
customer_name = models.CharField(max_length=40)
customer_email = models.EmailField(max_length=70,blank=True)
reseller = models.ForeignKey(Reseller, on_delete=models.CASCADE, null=True, blank=True, related_name='cust_children')
def __str__(self):
return self.customer_name
Django网址:
from django.urls import path
from . import views
urlpatterns = [
path('dashboard/', views.dashboard, name='dashboard'),
path('', views.loginpage, name='login'),
path('ViewReseller/<str:pk>', views.ViewReseller, name='ViewReseller'),
path('ViewCustomer/<str:pk>', views.ViewCustomer, name='ViewCustomer'),
]
Django视图:
@login_required(login_url='login')
def ViewReseller(request, pk):
resellers = Reseller.objects.all()
customers = Customer.objects.all()
reseller_details = Reseller.objects.get(id=pk)
context = { 'resellers' : resellers, 'customers' : customers, 'reseller_details' : reseller_details }
return render(request, 'main/dashboard/view_reseller.html', context)
@login_required(login_url='login')
def ViewCustomer(request, pk):
resellers = Reseller.objects.all()
customers = Customer.objects.all()
customer_details = Customer.objects.get(id=pk)
context = { 'resellers' : resellers, 'customers' : customers, 'customer_details' : customer_details }
return render(request, 'main/dashboard/view_customer.html', context)
Django HTML:
<ul id="tree1" >
{% recursetree resellers %}
<li><a href="{% url 'ViewReseller' node.id %}" class="menu-icon fa fa-home">
{{ node.reseller_name }}</a>
{% if not node.is_leaf_node %}
<ul class="children">
{{ children }}
</ul>
{% endif %}
{% if node.cust_children.all %}
<ul>
{% for customer in node.cust_children.all %}
<li><a href="{% url 'ViewCustomer' customer.id %}" class="menu-icon fa fa-user">
{{ customer.customer_name }}</a></li>
{% endfor %}
</ul>
{% endif %}
</li>
{% endrecursetree %}
</ul>
下面的屏幕快照显示了只有末端叶子节点如何响应单击时的页面呈现,而其余部分则没有: