url分发
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from django.db import models # Create your models here. class Author(models.Model): nid = models.AutoField(primary_key=True) name=models.CharField( max_length=32) age=models.IntegerField() # 与AuthorDetail建立一对一的关系 authorDetail=models.OneToOneField(to="AuthorDetail",on_delete=models.CASCADE) def __str__(self): return self.name class AuthorDetail(models.Model): nid = models.AutoField(primary_key=True) birthday=models.DateField() telephone=models.BigIntegerField() addr=models.CharField( max_length=64) class Publish(models.Model): nid = models.AutoField(primary_key=True) name=models.CharField( max_length=32) city=models.CharField( max_length=32) email=models.EmailField() def __str__(self): return self.name class Book(models.Model): nid = models.AutoField(primary_key=True) title = models.CharField( max_length=32) publishDate=models.DateField() price=models.DecimalField(max_digits=5,decimal_places=2) # 与Publish建立一对多的关系,外键字段建立在多的一方 publish=models.ForeignKey(to="Publish",to_field="nid",on_delete=models.CASCADE) # 与Author表建立多对多的关系,ManyToManyField可以建在两个模型中的任意一个,自动创建第三张表 authors=models.ManyToManyField(to='Author',) def __str__(self): return self.title
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
from django.db import models # Create your models here. class Order(models.Model): title=models.CharField(max_length=32)
一、url分发
def test01(request): return HttpResponse("test01") def test02(request): return HttpResponse("test02") def test03(request): return HttpResponse("test03") def test04(request): return HttpResponse("test04") def test05(request): return HttpResponse("test05")
1、一级分发
urlpatterns = [ url(r'^Xadmin/',([ url(r'^test01/',test01), url(r'^test02/', test02), url(r'^test03/', test03), ],None,None)) ]
2、二级分发
urlpatterns = [ url("^Xadmin/", ([ url(r'^test01/', ([ url(r'^test04/', test04), url(r'^test05/', test05), ], None, None)), url(r'^test02/', test02), url(r'^test03/', test03), ], None, None)), ]
上面把url路径写死了
我们注册的模型表都存放在admin.site._registry
通过类能拿到它所在app的名字和它模型表的名字
例:
3、修改后的一级路由
url(r'^Xadmin/', (get_urls(),None,None)),
def get_urls(): print(admin.site._registry) # {Book:modelAdmin(Book),.......} temp=[] for model,admin_class_obj in admin.site._registry.items(): app_name=model._meta.app_label model_name=model._meta.model_name temp.append(url(r'^{0}/{1}/'.format(app_name,model_name), test01,) return temp
4、修改后的二级路由 (对每一个模型表进行增删改查)
url(r'^Xadmin/', (get_urls(),None,None)),
def get_urls(): print(admin.site._registry) # {Book:modelAdmin(Book),.......} temp=[] for model,admin_class_obj in admin.site._registry.items(): app_name=model._meta.app_label model_name=model._meta.model_name temp.append(url(r'^{0}/{1}/'.format(app_name,model_name), (get_urls_2(),None,None)),) return temp def get_urls_2(): temp=[] temp.append(url(r"^$",list_view)) temp.append(url(r"^add/$",add_view)) temp.append(url(r"^(\d+)/change/$",change_view)) temp.append(url(r"^(\d+)/delete/$",delete_view)) return temp
增删改查的视图函数:
def list_view(request): return HttpResponse("list_view") def add_view(request): return HttpResponse("add_view") def change_view(request,id): return HttpResponse("change_view") def delete_view(request,id): return HttpResponse("delete_view")
经过二级分发的设置,我们就设计出了4(增\删\改\查)x7(Author\Authordetail\Publish\book\Order\auth_group\auth_user)=28个url路径