10.8Xadmin url注册

2018-10-8 16:45:31

这两天在弄我的服务器,就是是看各种教程 死活部署不好我的Django项目 博客网站

过几天再弄!

越努力,越幸运!永远不要高估自己!

 

通过两个方法,通过类变量找到字符串和app名字

 url的注册和分发!!

主要看用户怎样访问的!!!  xadmin 采用了面向对象! 两个类互相调用方法!

放上源码,.自己体会

url.py

"""s10day83 URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.conf.urls import url, include
    2. Add a URL to urlpatterns:  url(r'^blog/', include('blog.urls'))
"""
from django.conf.urls import url
from django.contrib import admin


from Xadmin.service.Xadmin import site
urlpatterns = [
    url(r'^admin/',  admin.site.urls),
    url(r'^Xadmin/', site.urls),  
  # url(r'^xx/', xxx) xxx部分要么是视图函数,要么得放([], none,none)分发url 正如上面site.urls执行这个方法
  # 返回的正好是一个元组
([], none,none) 所以可以执行
# django 启动的时候这个模块直接被加载,site.urls 是调用静态方法 urls是类里面的静态方法,调用的时候不需要在后面加()

]

Xadmin.py

from django.conf.urls import url
from django.shortcuts import HttpResponse,render,redirect
class ModelXadmin(object):
    def __init__(self,model,site):

        self.model=model
        self.site=site

    def list_view(self, request):
        # self.model 指代了用户当前要访问的表
        print("self.model",self.model)

        data_list=self.model.objects.all()
        print("data_list",data_list)
        return render(request, 'list_view.html',{"data_list":data_list})

    def add_view(self, request):
        return render(request, 'add_view.html')

    def change_view(self, request, id):
        return render(request, 'change_view.html')

    def delete_view(self, request, id):
        return render(request, 'delete_view.html')

    def get_urls2(self):
        temp = []

        temp.append(url(r"^$", self.list_view))
        temp.append(url(r"^add/$", self.add_view))
        temp.append(url(r"^(\d+)/change/$", self.change_view))
        temp.append(url(r"^(\d+)/delete/$", self.delete_view))

        return temp

    @property
    def urls2(self):
        return self.get_urls2(), None, None


class XadminSite(object):
    def __init__(self, name='admin'):
        self._registry = {}


    def get_urls(self):

        print(self._registry)  # {Book:modelAdmin(Book),.......}

        temp = []
        for model, admin_class_obj in self._registry.items():
       # 获取当前循环的model的字符串与所在app的名字 app_name
= model._meta.app_label  # "app01" model_name = model._meta.model_name # "book" temp.append(url(r'^{0}/{1}/'.format(app_name, model_name), admin_class_obj.urls2), ) ''' 当用户访问的时候,看到指的谁 url(r"app01/book",ModelXadmin(Book,site).urls2) url(r"app01/publish",ModelXadmin(Publish,site).urls2) url(r"app02/order",ModelXadmin(Order,site).urls2) ''' return temp @property def urls(self): return self.get_urls(),None,None def register(self, model, admin_class=None, **options): if not admin_class: admin_class = ModelXadmin self._registry[model] = admin_class(model, self) # {Book:ModelAdmin(Book),Publish:ModelAdmin(Publish)} site=XadminSite()

 

在Django一启动,就构建好了! 每一个分发都构建一个内存空间! 

 

posted @ 2018-10-08 16:49  我想喝杨枝甘露~  阅读(168)  评论(0编辑  收藏  举报