1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
arya 项目
01
(主要学习reverse方法,逆向解析url)
一、项目结构(只列出使用的文件)
-
learnmodelform 项目目录
-
app01 第一个app
model.py 数据库文件
arya.py 引用插件
-
arya 第二个app
apps.py 加载自动搜索模块,django启动时自动搜索该我文件配置的模块
-
service arya逻辑文件目录
v1.py 逻辑代码文件,主要用来配置url,table表单展示数据,数据库操作等
-
learnmodelform
settings.py 配置文件
urls.py 路由文件
-
templates 模板文件
model.py代码:
from
django.db
import
models
# Create your models here.
class
Role(models.Model):
caption
=
models.CharField(max_length
=
32
)
def
__str__(
self
):
return
self
.caption
class
UserType(models.Model):
title
=
models.CharField(max_length
=
32
)
def
__str__(
self
):
return
self
.title
class
UserInfo(models.Model):
name
=
models.CharField(max_length
=
32
)
pwd
=
models.CharField(max_length
=
32
)
email
=
models.CharField(max_length
=
32
)
ut
=
models.ForeignKey(to
=
"UserType"
, blank
=
True
,null
=
True
)
roles
=
models.ManyToManyField(to
=
"Role"
,blank
=
True
)
def
__str__(
self
):
return
self
.name
arya.py代码:
from
arya.service
import
v1
from
.
import
models
"""
{
models.UserInfo: AryaConfig(models.UserInfo,site),
models.UserType: AryaConfig(models.UserType,site),
}
"""
class
UserInfoConfig(v1.AryaConfig):
list_display
=
[
'name'
,
'pwd'
,
'id'
,
'email'
]
v1.site.register(models.UserInfo,UserInfoConfig)
class
UserTypeConfig(v1.AryaConfig):
list_display
=
[
'id'
,
'title'
]
v1.site.register(models.UserType, UserTypeConfig)
class
RoleConfig(v1.AryaConfig):
list_display
=
[
'id'
,
'caption'
]
v1.site.register(models.Role,RoleConfig)
apps.py代码(在arya应用中)
from
django.apps
import
AppConfig
class
AryaConfig(AppConfig):
name
=
'arya'
def
ready(
self
):
from
django.utils.module_loading
import
autodiscover_modules
autodiscover_modules(
'arya'
)
v1.py代码:
from
django.shortcuts
import
HttpResponse,render,redirect
from
django.utils.safestring
import
mark_safe
from
django.urls
import
reverse
class
AryaConfig(
object
):
list_display
=
[]
def
get_list_display(
self
):
result
=
[]
result.extend(
self
.list_display)
# 如果有编辑权限 #reslut=['name', 'pwd', 'id', 'email']
result.append(AryaConfig.row_edit)
# 如果有删除权限
result.append(AryaConfig.row_del)
result.insert(
0
, AryaConfig.row_checkbox)
return
result
def
row_del(
self
,row):
# return row.name + row.email+str(row.id)
# 生成删除URL
# app01_userinfo_delete
app
=
self
.model_class._meta.app_label
#app名
md
=
self
.model_class._meta.model_name
name
=
"arya:%s_%s_delete"
%
(app,md,)
# arya:app01_userinfo_delete
url
=
reverse(viewname
=
name, args
=
(row.
id
,))
#逆向解析url
print
(url)
return
mark_safe(
"<a href='{0}'>删除</a>"
.
format
(url))
def
row_edit(
self
,row):
# return row.name + row.email+str(row.id)
# 生成编辑URL
# app01_userinfo_change
app
=
self
.model_class._meta.app_label
md
=
self
.model_class._meta.model_name
name
=
"arya:%s_%s_change"
%
(app, md,)
# arya:app01_userinfo_change 必须按照这种格式,change是视图函数
url
=
reverse(viewname
=
name,args
=
(row.
id
,))
return
mark_safe(
"<a href='{0}'>编辑</a>"
.
format
(url))
def
row_checkbox(
self
,row):
return
mark_safe(
"<input type='checkbox' value='{0}' />"
.
format
(row.
id
))
def
__init__(
self
, model_class,site):
self
.model_class
=
model_class
self
.site
=
site
@
property
def
urls(
self
):
from
django.conf.urls
import
url, include
# self.model_class # UserInfo,UserGroup
app
=
self
.model_class._meta.app_label
md
=
self
.model_class._meta.model_name
partterns
=
[
url(r
'^$'
,
self
.changelist_view, name
=
"%s_%s_list"
%
(app, md,)),
url(r
'^add/'
,
self
.add_view,name
=
"%s_%s_add"
%
(app,md,)),
url(r
'^(\d+)/change/'
,
self
.change_view,name
=
"%s_%s_change"
%
(app,md,)),
url(r
'^(\d+)/delete/'
,
self
.delete_view,name
=
"%s_%s_delete"
%
(app,md,)),
]
return
partterns
def
changelist_view(
self
,request):
"""
访问列表页面
self = AryaConfig对象1(UserInfo),model_class=UserInfo
/userinfo/
/userinfo/add/
/userinfo/1/delete/
/userinfo/1/change/
self.model_class = UserInfo
/usertype/
/usertype/add/
/usertype/1/delete/
/usertype/1/change/
self.model_class = UserType
:param request:
:return:
"""
table_data
=
[]
queryset
=
self
.model_class.objects.
all
()
# [obj,obj,obj,obj]
# list_display = ['name','pwd','email']
for
obj
in
queryset:
row
=
[]
for
str_func
in
self
.get_list_display():
if
isinstance
(str_func,
str
):
col
=
getattr
(obj,str_func)
else
:
col
=
str_func(
self
,obj)
row.append(col)
table_data.append(row)
return
render(request,
'changelist.html'
,{
'table_data'
: table_data})
def
add_view(
self
,request):
return
HttpResponse(
'添加页面'
)
def
change_view(
self
,request,nid):
# obj = self.model_class.objects.filter(id=nid).first()
return
HttpResponse(
'修改页面'
)
def
delete_view(
self
,request,nid):
# self.model_class.objects.filter(id=nid).delete()
return
HttpResponse(
'删除页面'
)
class
AryaSite(
object
):
def
__init__(
self
):
self
._registry
=
{}
def
register(
self
, class_name, config_class):
"""
self._registry = {
models.UserInfo: obj1 = AryaConfig(models.UserInfo,site),
models.UserType: obj2 = AryaConfig(models.UserType,site),
}
:param class_name:
:param config_class:
:return:
"""
self
._registry[class_name]
=
config_class(class_name,
self
)
@
property
def
urls(
self
):
from
django.conf.urls
import
url, include
partterns
=
[
url(r
'^login/'
,
self
.login),
url(r
'^logout/'
,
self
.logout),
]
for
model_class,arya_config_obj
in
self
._registry.items():
app_model_name
=
r
'^{0}/{1}/'
.
format
(model_class._meta.app_label,model_class._meta.model_name)
#/app名/模块名/
pt
=
url(app_model_name, (arya_config_obj.urls,
None
,
None
))
partterns.append(pt)
return
partterns
def
login(
self
,request):
from
django.shortcuts
import
HttpResponse
return
HttpResponse(
'Login'
)
def
logout(
self
,request):
from
django.shortcuts
import
HttpResponse
return
HttpResponse(
'Logout'
)
site
=
AryaSite()
urls.py代码:
from
django.conf.urls
import
url,include
from
django.contrib
import
admin
from
arya.service
import
v1
urlpatterns
=
[
url(r
'^admin/'
, admin.site.urls),
url(r
'arya/'
, (v1.site.urls,
None
,
'arya'
)),
]
settings.py修改install_app
'arya.apps.AryaConfig'
,
'app01'
,
changelist.html 模板代码:
<!DOCTYPE html>
<html lang
=
"en"
>
<head>
<meta charset
=
"UTF-8"
>
<title>Title<
/
title>
<
/
head>
<body>
<table border
=
"1"
>
{
%
for
row
in
table_data
%
}
<tr>
{
%
for
col
in
row
%
}
<td>{{ col }}<
/
td>
{
%
endfor
%
}
<
/
tr>
{
%
endfor
%
}
<
/
table>
<
/
body>
<
/
html>
二、ModelForm插件用法(其他代码参考一)
views.py代码:
from
django.shortcuts
import
render,redirect
# Create your views here.
from
django.forms
import
ModelForm
from
.
import
models
class
TestModelForm(ModelForm):
class
Meta:
# model = models.UserInfo
model
=
models.UserType
fields
=
"__all__"
def
test(request):
if
request.method
=
=
"GET"
:
obj
=
TestModelForm()
return
render(request,
'test.html'
, {
'obj'
:obj})
else
:
obj
=
TestModelForm(data
=
request.POST)
if
obj.is_valid():
obj.save()
return
redirect(
'/arya/app01/usertype/'
)
return
render(request,
'test.html'
, {
'obj'
:obj})
def
edit(request,nid):
# user_obj = models.UserInfo.objects.filter(id=nid).first()
user_obj
=
models.UserType.objects.
filter
(
id
=
nid).first()
if
request.method
=
=
'GET'
:
obj
=
TestModelForm(instance
=
user_obj)
return
render(request,
'edit.html'
,{
'obj'
:obj})
else
:
obj
=
TestModelForm(instance
=
user_obj,data
=
request.POST)
if
obj.is_valid():
obj.save()
return
redirect(
'/arya/app01/usertype/'
)
return
render(request,
'edit.html'
, {
'obj'
:obj})
urls.py
from
django.conf.urls
import
url,include
from
django.contrib
import
admin
from
arya.service
import
v1
from
app01
import
views
urlpatterns
=
[
url(r
'^admin/'
, admin.site.urls),
url(r
'arya/'
, (v1.site.urls,
None
,
'arya'
)),
url(r
'^test/'
,views.test),
url(r
'^edit/(\d+)/'
,views.edit),
]
test.html
<!DOCTYPE html>
<html lang
=
"en"
>
<head>
<meta charset
=
"UTF-8"
>
<title>Title<
/
title>
<
/
head>
<body>
<h1>添加页面<
/
h1>
<form method
=
"POST"
novalidate>
{
%
csrf_token
%
}
{{ obj.as_p }}
<
input
type
=
"submit"
value
=
"提交"
>
<
/
form>
<
/
body>
<
/
html>
edit.html
<!DOCTYPE html>
<html lang
=
"en"
>
<head>
<meta charset
=
"UTF-8"
>
<title>Title<
/
title>
<
/
head>
<body>
<h1>编辑页面<
/
h1>
<form method
=
"POST"
novalidate>
{
%
csrf_token
%
}
{{ obj.as_p }}
<
input
type
=
"submit"
value
=
"提交"
>
<
/
form>
<
/
body>
<
/
html>
|
本文转自小白的希望 51CTO博客,原文链接:http://blog.51cto.com/haoyonghui/2044721
,如需转载请自行联系原作者