设计url 通过分发的方式 Xadmin_demo

urlpatterns = [
url(r'^Xadmin/',([
          url(r'^add/$', views.add)
          url(r'^delete/$', views.delete)
          ], None,None)),
这样就完成了一级分发

然后进行自行设计url
需求 3张表,增删改查, 12个url

127.0.0.1:8000/Xadmin/app01/add/
127.0.0.1:8000/Xadmin/app01/delete/


在urls.py编辑

def list_view(request):
return HttpResponse('userindex')
def add_view(request):
return HttpResponse('user_add')
def change_view(request, id):
return HttpResponse('user_change')
def delete_view(request, id):
return HttpResponse('user_delete')

def has_urls():
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))
print(temp)
return temp
def get_urls():
print(admin.site._registry)
temp = []
for model,admin_class_obj in admin.site._registry.items():
app_name = model._meta.app_label
model_name = model._meta.model_name
print(app_name,model_name)
temp.append(url(r'^{0}/{1}/'.format(app_name,model_name),(has_urls(), None,None)),)
return temp
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^Xadmin/',(get_urls(), None,None)),]

settings.py
设置
INSTALLED_APPS = [
'app01.apps.App01Config',

'app02.apps.App02Config',
]

app01项目下的admin.py
from django.contrib import admin
from app01 import models
# Register your models here.
admin.site.register(models.Book)
admin.site.register(models.Publish)

app01项目下的models.py


app02项目下的admin.py
from django.contrib import admin
from app02 import models
# Register your models here.
admin.site.register(models.Order)

app01项目下的models.py
from django.db import models
class Order(models.Model):
title = models.CharField(max_length=32)
def __str__(self):
return self.title



posted @ 2018-08-31 22:51  想翻身的猫  阅读(446)  评论(0编辑  收藏  举报