CRM


路由分发

本质:
url(r'^test/',([],None,None))

一级分发
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^test/',([
url(r'^test_1',test_1),
url(r'^test_2',test_2)
],None,None))
]

二级分发
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^test/',([
url(r'^test_1/',([
url(r'^test_1_1',test_1_1),
url(r'^test_1_2',test_1_2),
url(r'^test_1_3',test_1_3),
],None,None)),
url(r'^test_2',test_2)
],None,None))
]


url设计
# 如何获取模型表所在的应用名
app_label = model_class._meta.app_label
# 如何获取模型表字符串小写名
model_name = model_class._meta.model_name

手动实现模型表的增删改查四条url
from django.conf.urls import url
from django.contrib import admin
from django.shortcuts import HttpResponse

def test(request):
return HttpResponse('test')
def test_1(request):
return HttpResponse('test_1')
def test_2(request):
return HttpResponse('test_2')

def test_1_1(request):
return HttpResponse('test_1_1')
def test_1_2(request):
return HttpResponse('test_1_2')
def test_1_3(request):
return HttpResponse('test_1_3')
def index(request):
return HttpResponse('index')

def list_view(request):
return HttpResponse('list_view')
def add_view(request):
return HttpResponse('add_view')
def edit_view(request,id):
return HttpResponse('edit_view')
def delete_view(request,id):
return HttpResponse('delete_view')

def get_urls2():
tmp = [
url(r'^$',list_view),
url(r'^add/',add_view),
url(r'^edit/(\d+)/',edit_view),
url(r'^delete/(\d+)/',delete_view),
]
return tmp,None,None


def get_urls():
tmp = []
for model_class,config_obj in admin.site._registry.items():
# 如何获取模型表所在的应用名
app_label = model_class._meta.app_label
# 如何获取模型表字符串小写名
model_name = model_class._meta.model_name
# print(app_label,model_name)
tmp.append(
url(r'^%s/%s/'%(app_label,model_name),get_urls2())
)

return tmp
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^test/',([
url(r'^test_1/',([
url(r'^test_1_1',test_1_1),
url(r'^test_1_2',test_1_2),
url(r'^test_1_3',test_1_3),
],None,None)),
url(r'^test_2',test_2)
],None,None)),
url(r'^stark/',(get_urls(),None,None))
]

stark组件(完全仿造django admin)
组件也是应用,一个应用可以用在多个项目里,一个项目也可以多个应用

启动:
from django.apps import AppConfig
from django.utils.module_loading import autodiscover_modules


class StarkConfig(AppConfig):
name = 'stark'

def ready(self):
return autodiscover_modules('stark')

注册:

from app01 import models
models.Book._meta.get_field('title').verbose_name
'书名'
models.Book._meta.get_field('price').verbose_name
'price'
models.Book._meta.get_field('price')
<django.db.models.fields.DecimalField: price>


前后端取消转义
前端:|safe
后端:mark_safe()

posted @ 2019-03-26 20:46  彼岸花纽约  阅读(120)  评论(0编辑  收藏  举报