Skip to content

Commit d2a82a9

Browse files
author
root
committed
自动路由时,优先使用人工开发的同名视图(ModelActionView)
1 parent cb90a1a commit d2a82a9

28 files changed

+18007
-173
lines changed

apps/a/templates/a/_menu.html

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
3+
{% ifmenu %}
4+
<li>
5+
<a><i class="fa fa-book"></i> <span class="nav-label">演示</span>
6+
<span class="fa arrow"> </span></a>
7+
<ul class="nav nav-second-level">
8+
<li>{% show_menu_url '网站用户' 'a:user_list' 'auth.view_user' %}</li>
9+
<li>{% show_menu_url 'b' 'a:b_list' 'a.view_b' %}</li>
10+
<li>{% show_menu_url 'o2o' 'a:one_list' 'a.view_one' %}</li>
11+
<li>{% show_menu_url 'p1' 'a:p_list' 'a.view_p' %}</li>
12+
<li>{% show_menu_url 'p扩展2' 'a:p2_list' 'a.view_p2' %}</li>
13+
<li>{% show_menu_url 'p扩展3' 'a:p3_list' 'a.view_p3' %}</li>
14+
<li>{% show_menu_url 't' 'a:t_list' 'a.view_t' %}</li>
15+
<li>{% show_menu_url 'm' 'a:m_list' 'a.view_m' %}</li>
16+
<li>{% show_menu_url 'm2t' 'a:m2t_list' 'a.view_m2t' %}</li>
17+
18+
</ul>
19+
</li>
20+
{% endifmenu %}

apps/a/urls.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
except Exception:
66
from django.conf.urls import url as re_path # django 1.*
77

8-
# from generic.routers import add_router_for_all_models
98
from generic.routers import MyRouter
109
from . import models
1110
from . import views
@@ -14,40 +13,40 @@
1413

1514

1615
# User
17-
re_path(r'^user/$', views.UserList.as_view(), name="user_list"),
16+
re_path(r'^user/$', views.UserListView.as_view(), name="user_list"),
1817
*MyRouter(
1918
models.User,
2019
list=False,
2120
delete=False,
2221
),
2322

2423
# One
25-
re_path(r'^one/$', views.OneList.as_view(), name="one_list"),
24+
re_path(r'^one/$', views.OneListView.as_view(), name="one_list"),
2625
*MyRouter(models.One, detail=False),
2726

2827
# P
29-
re_path(r'^p/$', views.PList.as_view(), name="p_list"),
28+
re_path(r'^p/$', views.PListView.as_view(), name="p_list"),
3029
*MyRouter(models.P),
3130

32-
re_path(r'^p2/$', views.P2List.as_view(), name="p2_list"),
31+
re_path(r'^p2/$', views.P2ListView.as_view(), name="p2_list"),
3332
*MyRouter(models.P2),
3433

35-
re_path(r'^p3/$', views.P3List.as_view(), name="p3_list"),
34+
re_path(r'^p3/$', views.P3ListView.as_view(), name="p3_list"),
3635
*MyRouter(models.P3, create=False),
3736

3837

3938
# T
40-
re_path(r'^t/$', views.TList.as_view(), name="t_list"),
39+
re_path(r'^t/$', views.TListView.as_view(), name="t_list"),
4140
*MyRouter(models.T),
4241

4342

4443
# M
45-
re_path(r'^m/$', views.MList.as_view(), name="m_list"),
44+
re_path(r'^m/$', views.MListView.as_view(), name="m_list"),
4645
*MyRouter(models.M),
4746

4847

4948
# M2T
50-
re_path(r'^m2t/$', views.M2TList.as_view(), name="m2t_list"),
49+
re_path(r'^m2t/$', views.M2TListView.as_view(), name="m2t_list"),
5150
*MyRouter(
5251
models.M2T,
5352
# delete=False,

apps/a/views.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class UserMixin:
88
model = models.User
99

1010

11-
class UserList(UserMixin, views.MyListView):
11+
class UserListView(UserMixin, views.MyListView):
1212
list_fields = [
1313
# 'id',
1414
'username',
@@ -28,7 +28,7 @@ class OneMixin:
2828
model = models.One
2929

3030

31-
class OneList(OneMixin, views.MyListView):
31+
class OneListView(OneMixin, views.MyListView):
3232
paginate_by = 2 # 每页条数
3333
list_fields = [
3434
'name',
@@ -45,7 +45,7 @@ class PMixin:
4545
model = models.P
4646

4747

48-
class PList(PMixin, views.MyListView):
48+
class PListView(PMixin, views.MyListView):
4949
list_fields = [
5050
'name',
5151
# # 'desc',
@@ -61,7 +61,7 @@ def get_queryset(self):
6161
return qs
6262

6363

64-
class P2List(views.MyListView):
64+
class P2ListView(views.MyListView):
6565
model = models.P2
6666
list_fields = [
6767
'name2',
@@ -70,7 +70,7 @@ class P2List(views.MyListView):
7070
]
7171

7272

73-
class P3List(views.MyListView):
73+
class P3ListView(views.MyListView):
7474
model = models.P3
7575
list_fields = [
7676
'name3',
@@ -79,7 +79,7 @@ class P3List(views.MyListView):
7979
]
8080

8181

82-
class TList(views.MyListView):
82+
class TListView(views.MyListView):
8383
model = models.T
8484
paginate_by = 5 # 每页条数
8585
list_fields = [
@@ -98,7 +98,7 @@ class MMixin:
9898
model = models.M
9999

100100

101-
class MList(MMixin, views.MyListView):
101+
class MListView(MMixin, views.MyListView):
102102
# paginate_by = None # 不分页
103103
filter_fields = [('name', 'M名称'), ('t__name', 'T名称'), ] # 搜索过滤
104104
filter_orm_fields = ['t__name__icontains', 't__p__name__icontains', ] # orm过滤字段, 为安全必须配置字段
@@ -130,7 +130,7 @@ class M2TMixin:
130130
model = models.M2T
131131

132132

133-
class M2TList(M2TMixin, views.MyListView):
133+
class M2TListView(M2TMixin, views.MyListView):
134134
list_fields = [
135135
'id',
136136
# 'm',

apps/allapp.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
app_urls = import_module(f'{app_name}.urls')
2626
except Exception as e:
2727
if getattr(e, 'name', None) != f'{app}.urls':
28+
if getattr(settings, 'RUNTIME_RAISE_APP_ERROR', True):
29+
raise
2830
logger.error(e, exc_info=True)
2931
continue
3032
URLS_APPS[app_name] = app_urls

apps/b/templates/b/_menu.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
3+
4+
{% ifmenu %}
5+
<li>
6+
<a><i class="fa fa-book"></i> <span class="nav-label">大数据游标</span>
7+
<span class="fa arrow"> </span></a>
8+
<ul class="nav nav-second-level">
9+
<li>{% show_menu_url '主机(非Null排序)' 'b:host_list' 'b.view_host' %}</li>
10+
<li>{% show_menu_url '用户(支持Null排序)' 'b:user_list' 'b.view_user' %}</li>
11+
12+
<!-- <li>
13+
<a><span class="nav-label">ID排序</span> <span class="fa arrow"> </span></a>
14+
<ul class="nav nav-third-level">
15+
<li><a href="/b/host/?ordering=id">主机</a></li>
16+
<li><a href="/b/user/?ordering=id">用户</a></li>
17+
</ul>
18+
</li> -->
19+
20+
</ul>
21+
</li>
22+
{% endifmenu %}

apps/b/urls.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55
except Exception:
66
from django.conf.urls import url as re_path # django 1.*
77

8-
from generic.routers import add_router_for_all_models
9-
# from generic.routers import MyRouter
8+
from generic.routers import MyRouter
109

1110
from . import views
1211

1312
urlpatterns = [
1413

15-
re_path(r'^user/$', views.UserList.as_view(), name="user_list"),
16-
re_path(r'^host/$', views.HostList.as_view(), name="host_list"),
14+
re_path(r'^user/$', views.UserListView.as_view(), name="user_list"),
15+
re_path(r'^host/$', views.HostListView.as_view(), name="host_list"),
1716

1817

1918
]
2019

2120

22-
add_router_for_all_models()
21+
MyRouter.add_router_for_all_models()
2322

2423
# print(urlpatterns)

apps/b/views.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Create your views here.
66

77

8-
class UserList(views.MyListView):
8+
class UserListView(views.MyListView):
99
model = models.User
1010
list_fields = [
1111
'id',
@@ -22,7 +22,7 @@ class UserList(views.MyListView):
2222
filter_fields = ['name', ] # 搜索过滤
2323

2424

25-
class HostList(views.MyListView):
25+
class HostListView(views.MyListView):
2626
model = models.Host
2727
list_fields = [
2828
'id',

0 commit comments

Comments
 (0)