栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Django:动态添加应用程序作为插件,自动构建网址和其他设置

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Django:动态添加应用程序作为插件,自动构建网址和其他设置

您想要的是这样的东西:

from django.utils.importlib import import_modulefor app in settings.INSTALLED_APPS:    try:        mod = import_module('%s.urls' % app)        # possibly cleanup the after the imported module?        #  might fuss up the `include(...)` or leave a polluted namespace    except:        # cleanup after module import if fails,        #  maybe you can let the `include(...)` report failures        pass    else:        urlpatterns += patterns('', url(r'^%s/' % slugify(app), include('%s.urls' % app)        )

您还将想要

slugify
从django模板或utils中窃取并实现自己的工具(我现在不太确定它们现在在哪里吗?),并对其稍加修改以去除任何“点”和其他不需要的命名空间在您的“网址”中,例如,您可能不希望自己的网址看起来像这样:“
example.com/plugin.some_plugin_appname/”,但喜欢
example.com/nice_looking_appname/

您甚至可能根本不想为其自动命名,而是在插件自己的“设置”模块中进行可配置的“设置”或类似的操作:

# plugin settings confurl_namespace = 'my_nice_plugin_url/'# root urls.py:url(r'^%s/' % mod.url_namespace, include(...))# or:url(r'^%s/' % app.settings.url_namespace, inc..

您可能会明白要点。

亲切的问候,



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/625871.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号