1. 程式人生 > >設計url 通過分發的方式 Xadmin_demo

設計url 通過分發的方式 Xadmin_demo

dem response max sel resp dmi apps d+ emp

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



設計url 通過分發的方式 Xadmin_demo