加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Django admin

发布时间:2020-12-15 17:17:27 所属栏目:大数据 来源:网络整理
导读:Django admin使用 admin app Django 提供了基于 web 的管理工具。 Django 自动管理工具是 django.contrib 的一部分。你可以在项目的 settings.py 中的 INSTALLED_APPS 看到它: settings.py中 INSTALLED_APPS = django.contrib是一套庞大的功能集,它是Djang

Django admin使用

admin app

Django 提供了基于 web 的管理工具。

Django 自动管理工具是 django.contrib 的一部分。你可以在项目的 settings.py 中的 INSTALLED_APPS 看到它:

settings.py中

INSTALLED_APPS =

django.contrib是一套庞大的功能集,它是Django基本代码的组成部分。

启用admin管理工具

要启动admin管理工具,只需要在 urls.py 中启动admin对应的路由配置项即可。

通常我们正在使用PyCharm生成项目时会在 urls.py 中自动设置好。

django.conf.urls django.contrib urlpatterns =<span style="color: #000000;"> [
url(r
<span style="color: #800000;">'
<span style="color: #800000;">^admin/
<span style="color: #800000;">'
<span style="color: #000000;">,admin.site.urls),]

使用admin管理工具

首先先启动我们的Django项目,然后在浏览器中访问 http://127.0.0.1:8000/admin/,就能看到登陆界面。(具体网址和端口根据自己环境输入)

使用?python manage.py createsuperuser?来创建管理员账号,来登录admin管理后台。

想要在admin 界面管理某个数据模型(model),我们需要先将该数据模型(model)注册到 admin。

在admin.py中只要按照上面的方式将某个模型类(model class)注册,即可在Admin中实现该model对应的数据表增删改查的功能。

admin的定制

这种方式的注册比较简单,如果想要进行更多的定制操作,需要利用ModelAdmin进行操作,如:

= (,admin.site.register(models.Publisher,PublisherAdmin)

<span style="color: #008000;">#<span style="color: #008000;"> 注册方式2
<span style="color: #000000;">@admin.register(models.Book)
<span style="color: #0000ff;">class<span style="color: #000000;"> BookAdmin(admin.ModelAdmin):
list_display = (<span style="color: #800000;">"<span style="color: #800000;">title<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">price<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">publish_date<span style="color: #800000;">",<span style="color: #800000;">"<span style="color: #800000;">publisher<span style="color: #800000;">")

ModelAdmin中提供了大量的可定制功能,如

?1.?list_display,列表时,定制显示的列。

= (,,</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; xxxxx(self,obj): </span><span style="color: #0000ff;"&gt;return</span> <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;xxxxx</span><span style="color: #800000;"&gt;"</span></pre>

2.?list_display_links,列表时,定制列可以点击跳转。

= (,= (,)

3.?list_filter,列表时,定制右侧快速筛选。

4.?list_select_related,列表时,连表查询是否自动select_related

5. list_editable,列表时,可以编辑的列?

= (,= (,)

6.?search_fields,列表时,模糊搜索的功能

search_fields </span>= (<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;user</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;pwd</span><span style="color: #800000;"&gt;'</span>)</pre>

7.?date_hierarchy,列表时,对Date和DateTime类型进行搜索

date_hierarchy </span>= <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;ctime</span><span style="color: #800000;"&gt;'</span></pre>

8 . inlines,详细页面,如果有其他表和当前表做FK,那么详细页面可以进行动态增加和删除

UserInfoInline(admin.StackedInline): extra ==<span style="color: #0000ff;">class<span style="color: #000000;"> GroupAdminMode(admin.ModelAdmin):
list_display
= (<span style="color: #800000;">'
<span style="color: #800000;">id
<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">title<span style="color: #800000;">'<span style="color: #000000;">,)
inlines = [UserInfoInline,]

9 action,列表时,定制action中的操作

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 定制Action行为具体方法</span> <span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; func(self,request,queryset): </span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(self,queryset) </span><span style="color: #0000ff;"&gt;print</span>(request.POST.getlist(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;_selected_action</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;)) func.short_description </span>= <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;中文显示自定义Actions</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt; actions </span>=<span style="color: #000000;"&gt; [func,] </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; Action选项都是在页面上方显示</span> actions_on_top =<span style="color: #000000;"&gt; True </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; Action选项都是在页面下方显示</span> actions_on_bottom =<span style="color: #000000;"&gt; False </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 是否显示选择个数</span> actions_selection_counter = True</pre>

---------------------------------------------------------------------------------------------------------------------------------

10. 定制HTML模板

add_form_template ====== None

11. raw_id_fields,详细页面,针对FK和M2M字段变成以Input框形式

raw_id_fields </span>= (<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;FK字段</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;M2M字段</span><span style="color: #800000;"&gt;'</span>,)</pre>

12. fields,详细页面时,显示字段的字段

= (,)

13. exclude,详细页面时,排除的字段

= (,)

14. readonly_fields,详细页面时,只读字段

= (,)

15. fieldsets,详细页面时,使用fieldsets标签对数据进行分割显示

=: (,: (,,), : (,

16. 详细页面时,M2M显示时,数据移动选择(方向:上下和左右)

= (,)

17. ordering,列表时,数据排序规则

= ( [,]

18.?radio_fields,详细页面时,使用radio显示选项(FK默认使用select)

radio_fields = {: admin.VERTICAL}

19. form = ModelForm,用于定制用户请求时候表单验证

app01 django.forms django.forms <span style="color: #0000ff;">class<span style="color: #000000;"> MyForm(ModelForm):
others
=<span style="color: #000000;"> fields.CharField()

</span><span style="color: #0000ff;"&gt;class</span><span style="color: #000000;"&gt; Meta:
    model </span>= models =<span style="color: #000000;"&gt; models.UserInfo
    fields </span>= <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;__all__</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;

@admin.register(models.UserInfo)
<span style="color: #0000ff;">class<span style="color: #000000;"> UserAdmin(admin.ModelAdmin):

form </span>= MyForm</pre>

20. empty_value_display = "列数据为空时,显示默认值"

= list_display </span>= (<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;user</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;up</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;) </span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; up(self,obj): </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; obj.user up.empty_value_display </span>= <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;指定列数据为空时,默认显示</span><span style="color: #800000;"&gt;"</span></pre>
django.contrib <span style="color: #008000;">#<span style="color: #008000;"> Register your models here.

<span style="color: #0000ff;">from .models <span style="color: #0000ff;">import *

<span style="color: #0000ff;">class BookInline(admin.StackedInline): <span style="color: #008000;">#<span style="color: #008000;"> TabularInline
extra =<span style="color: #000000;"> 0
model =<span style="color: #000000;"> Book

<span style="color: #0000ff;">class<span style="color: #000000;"> BookAdmin(admin.ModelAdmin):

list_display </span>= (<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;title</span><span style="color: #800000;"&gt;"</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;publishDate</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;price</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;foo</span><span style="color: #800000;"&gt;"</span>,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;publisher</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)
list_display_links </span>= (<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;publishDate</span><span style="color: #800000;"&gt;'</span>,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;price</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)
list_filter </span>= (<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;price</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;,)
list_editable</span>=(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;title</span><span style="color: #800000;"&gt;"</span>,<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;publisher</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)
search_fields </span>= (<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;title</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;,)
date_hierarchy </span>= <span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;publishDate</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;
preserve_filters</span>=<span style="color: #000000;"&gt;False

</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; foo(self,obj):

    </span><span style="color: #0000ff;"&gt;return</span> obj.title+<span style="color: #000000;"&gt;str(obj.price)



</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 定制Action行为具体方法</span>
<span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; func(self,queryset)
    </span><span style="color: #0000ff;"&gt;print</span>(request.POST.getlist(<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;_selected_action</span><span style="color: #800000;"&gt;'</span><span style="color: #000000;"&gt;))

func.short_description </span>= <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;中文显示自定义Actions</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;
actions </span>=<span style="color: #000000;"&gt; [func,]
</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; Action选项都是在页面上方显示</span>
actions_on_top =<span style="color: #000000;"&gt; True
</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; Action选项都是在页面下方显示</span>
actions_on_bottom =<span style="color: #000000;"&gt; False

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 是否显示选择个数</span>
actions_selection_counter =<span style="color: #000000;"&gt; True



change_list_template</span>=<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;my_change_list_template.html</span><span style="color: #800000;"&gt;"</span>

<span style="color: #0000ff;">class<span style="color: #000000;"> PublishAdmin(admin.ModelAdmin):
list_display = (<span style="color: #800000;">'<span style="color: #800000;">name<span style="color: #800000;">',<span style="color: #800000;">'<span style="color: #800000;">email<span style="color: #800000;">'<span style="color: #000000;">,)
inlines =<span style="color: #000000;"> [BookInline,]

admin.site.register(Book,BookAdmin) <span style="color: #008000;">#<span style="color: #008000;"> 第一个参数可以是列表
<span style="color: #000000;">admin.site.register(Publish,PublishAdmin)
admin.site.register(Author)

Django admin源码解析

单例模式

单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在。当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场。

比如,某个服务器程序的配置信息存放在一个文件中,客户端通过一个 AppConfig 的类来读取配置文件的信息。如果在程序运行期间,有很多地方都需要使用配置文件的内容,也就是说很多地方都需要创建 AppConfig 对象的实例,这就导致系统中存在多个 AppConfig 的实例对象,而这样会严重浪费内存资源,尤其是在配置文件内容很多的情况下。事实上,类似 AppConfig 这样的类,我们希望在程序运行期间只存在一个实例对象。

在 Python 中,我们可以用多种方法来实现单例模式:

  • 使用 __new__()
  • 使用模块
  • 使用装饰器(decorator)
  • 使用元类(metaclass)

为了使类只能出现一个实例,我们可以使用?__new__()?来控制实例的创建过程,代码如下:

__new__()方法用来创建实例对象

__init__()方法用来初始化实例对象

==<span style="color: #0000ff;">class<span style="color: #000000;"> Singleton(object):
_instance
=<span style="color: #000000;"> None

</span><span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__new__</span>(cls,*args,**<span style="color: #000000;"&gt;kwargs):
    </span><span style="color: #0000ff;"&gt;print</span>(1<span style="color: #000000;"&gt;)
    </span><span style="color: #0000ff;"&gt;if</span> <span style="color: #0000ff;"&gt;not</span><span style="color: #000000;"&gt; cls._instance:
        cls._instance </span>= super(Singleton,cls).<span style="color: #800080;"&gt;__new__</span><span style="color: #000000;"&gt;(cls)
    </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; cls._instance

</span><span style="color: #0000ff;"&gt;def</span> <span style="color: #800080;"&gt;__init__</span><span style="color: #000000;"&gt;(self,age):
    </span><span style="color: #0000ff;"&gt;print</span>(2<span style="color: #000000;"&gt;)
    self.name </span>=<span style="color: #000000;"&gt; name
    self.age </span>=<span style="color: #000000;"&gt; age

<span style="color: #0000ff;">if <span style="color: #800080;">name == <span style="color: #800000;">'<span style="color: #800000;">main<span style="color: #800000;">'<span style="color: #000000;">:
p1 = Person(<span style="color: #800000;">"<span style="color: #800000;">deng<span style="color: #800000;">",18<span style="color: #000000;">)
p2 = Person(<span style="color: #800000;">"<span style="color: #800000;">deng<span style="color: #800000;">",18<span style="color: #000000;">)

</span><span style="color: #0000ff;"&gt;print</span>(p1 ==<span style="color: #000000;"&gt; p2)
</span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(id(p1),id(p2))

</span><span style="color: #0000ff;"&gt;print</span>(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;=</span><span style="color: #800000;"&gt;"</span> * 120<span style="color: #000000;"&gt;)

s1 </span>= Singleton(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;deng</span><span style="color: #800000;"&gt;"</span>,18<span style="color: #000000;"&gt;)
s2 </span>= Singleton(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;deng</span><span style="color: #800000;"&gt;"</span>,18<span style="color: #000000;"&gt;)
</span><span style="color: #0000ff;"&gt;print</span>(s1 ==<span style="color: #000000;"&gt; s2)
</span><span style="color: #0000ff;"&gt;print</span>(id(s1),id(s2))</pre>

使用模块方式

其实,Python 的模块就是天然的单例模式,因为模块在第一次导入时,会生成 .pyc 文件,当第二次导入时,就会直接加载 .pyc 文件,而不会再次执行模块代码。因此,我们只需把相关的函数和数据定义在一个模块中,就可以获得一个单例对象了。

如果我们真的想要一个单例类,可以考虑这样做:

==p1 = Singleton(<span style="color: #800000;">"<span style="color: #800000;">deng<span style="color: #800000;">",18)

使用下面的代码测试一下:

singleton <span style="color: #0000ff;">print<span style="color: #000000;">(id(p1))
<span style="color: #0000ff;">print<span style="color: #000000;">(p1.name)
p1.name = <span style="color: #800000;">"<span style="color: #800000;">Bob<span style="color: #800000;">"

<span style="color: #0000ff;">from singleton <span style="color: #0000ff;">import<span style="color: #000000;"> p1

<span style="color: #0000ff;">print<span style="color: #000000;">(id(p1))
<span style="color: #0000ff;">print(p1.name)

admin执行流程

1. 循环加载执行所有已经注册的app中的admin.py文件

,register_to=site)

2. 执行代码

<span style="color: #0000ff;">class<span style="color: #000000;"> BookAdmin(admin.ModelAdmin):
list_display = (<span style="color: #800000;">"<span style="color: #800000;">title<span style="color: #800000;">",<span style="color: #800000;">'<span style="color: #800000;">price<span style="color: #800000;">'<span style="color: #000000;">)

admin.site.register(Book,BookAdmin)
admin.site.register(Publish)

3. admin。site

这里应用的是一个单例模式,对于AdminSite类的一个单例模式,执行的每一个app中的每一个admin.site都是一个对象

4. 执行register方法

ModelAdmin(BaseModelAdmin):

<span style="color: #0000ff;">def register(self,model_or_iterable,admin_class=None,**<span style="color: #000000;">options):
<span style="color: #0000ff;">if <span style="color: #0000ff;">not<span style="color: #000000;"> admin_class:
admin_class =<span style="color: #000000;"> ModelAdmin
<span style="color: #008000;">#<span style="color: #008000;"> Instantiate the admin class to save in the registry
self._registry[model] = admin_class(model,self)

思考:在每一个app的admin .py中加上

(admin.site._registry)

到这里,注册结束!

5. admin的URL配置

urlpatterns =
</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; get_urls(self): </span><span style="color: #0000ff;"&gt;from</span> django.conf.urls <span style="color: #0000ff;"&gt;import</span><span style="color: #000000;"&gt; url,include urlpatterns </span>=<span style="color: #000000;"&gt; [] </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; Add in each model's views,and create a list of valid URLS for the</span> <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; app_index</span> valid_app_labels =<span style="color: #000000;"&gt; [] </span><span style="color: #0000ff;"&gt;for</span> model,model_admin <span style="color: #0000ff;"&gt;in</span><span style="color: #000000;"&gt; self._registry.items(): urlpatterns </span>+=<span style="color: #000000;"&gt; [ url(r</span><span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;^%s/%s/</span><span style="color: #800000;"&gt;'</span> %<span style="color: #000000;"&gt; (model._meta.app_label,model._meta.model_name),include(model_admin.urls)),] </span><span style="color: #0000ff;"&gt;if</span> model._meta.app_label <span style="color: #0000ff;"&gt;not</span> <span style="color: #0000ff;"&gt;in</span><span style="color: #000000;"&gt; valid_app_labels: valid_app_labels.append(model._meta.app_label) </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; urlpatterns @property </span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; urls(self): </span><span style="color: #0000ff;"&gt;return</span> self.get_urls(),<span style="color: #800000;"&gt;'</span><span style="color: #800000;"&gt;admin</span><span style="color: #800000;"&gt;'</span>,self.name</pre>

6. url()方法的扩展应用

django.shortcuts HttpResponse(<span style="color: #0000ff;">def<span style="color: #000000;"> test02(request):
<span style="color: #0000ff;">return
HttpResponse(<span style="color: #800000;">"
<span style="color: #800000;">test02
<span style="color: #800000;">"
<span style="color: #000000;">)

urlpatterns =<span style="color: #000000;"> [
url(r<span style="color: #800000;">'<span style="color: #800000;">^admin/<span style="color: #800000;">'<span style="color: #000000;">,url(r<span style="color: #800000;">'<span style="color: #800000;">^yuan/<span style="color: #800000;">'<span style="color: #000000;">,([
url(r<span style="color: #800000;">'<span style="color: #800000;">^test01/<span style="color: #800000;">'<span style="color: #000000;">,test01),url(r<span style="color: #800000;">'<span style="color: #800000;">^test02/<span style="color: #800000;">'<span style="color: #000000;">,test02),],None,None)),]

<div class="cnblogs_code" onclick="cnblogs_code_show('8ef30d08-7682-472f-80de-71b438b8744e')">
<img id="code_img_closed_8ef30d08-7682-472f-80de-71b438b8744e" class="code_img_closed" src="https://www.52php.cn/res/2019/02-14/22/1c53668bcee393edac0d7b3b3daff1ae.gif" alt=""><img id="code_img_opened_8ef30d08-7682-472f-80de-71b438b8744e" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('8ef30d08-7682-472f-80de-71b438b8744e',event)" src="https://www.52php.cn/res/2019/02-14/22/405b18b4b6584ae338e0f6ecaf736533.gif" alt=""><div id="cnblogs_code_open_8ef30d08-7682-472f-80de-71b438b8744e" class="cnblogs_code_hide">

 django.conf.urls  django.contrib <span style="color: #0000ff;">from django.shortcuts <span style="color: #0000ff;">import<span style="color: #000000;"> HttpResponse

<span style="color: #0000ff;">def<span style="color: #000000;"> change_list_view(request):
<span style="color: #0000ff;">return HttpResponse(<span style="color: #800000;">"<span style="color: #800000;">change_list_view<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">def<span style="color: #000000;"> add_view(request):
<span style="color: #0000ff;">return HttpResponse(<span style="color: #800000;">"<span style="color: #800000;">add_view<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">def<span style="color: #000000;"> delete_view(request):
<span style="color: #0000ff;">return HttpResponse(<span style="color: #800000;">"<span style="color: #800000;">delete_view<span style="color: #800000;">"<span style="color: #000000;">)
<span style="color: #0000ff;">def<span style="color: #000000;"> change_view(request):
<span style="color: #0000ff;">return HttpResponse(<span style="color: #800000;">"<span style="color: #800000;">change_view<span style="color: #800000;">"<span style="color: #000000;">)

<span style="color: #0000ff;">def<span style="color: #000000;"> get_urls():

temp</span>=<span style="color: #000000;"&gt;[
    url(r</span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;^$</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;.format(app_name,model_name),change_list_view),url(r</span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;^add/$</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;.format(app_name,add_view),url(r</span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;^d+/del/$</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;.format(app_name,delete_view),url(r</span><span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;^d+/change/$</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;.format(app_name,change_view),]

</span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; temp

url_list=<span style="color: #000000;">[]

<span style="color: #0000ff;">for model_class,obj <span style="color: #0000ff;">in<span style="color: #000000;"> admin.site._registry.items():

model_name</span>=<span style="color: #000000;"&gt;model_class._meta.model_name
app_name</span>=<span style="color: #000000;"&gt;model_class._meta.app_label

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; temp=url(r"{0}/{1}/".format(app_name,(get_urls(),None))</span>
temp=url(r<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;{0}/{1}/</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;.format(app_name,include(get_urls()))
url_list.append(temp)

urlpatterns =<span style="color: #000000;"> [
url(r<span style="color: #800000;">'<span style="color: #800000;">^admin/<span style="color: #800000;">'<span style="color: #000000;">,(url_list,]

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读