首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Django中修改运行时的urlpattern

在Django中修改运行时的urlpattern
EN

Stack Overflow用户
提问于 2012-03-30 06:36:22
回答 2查看 2.5K关注 0票数 7

我正在开发一个需要加载动态模块(在运行时)的Django应用程序。现在我可以上传(从客户端浏览器到服务器)“插件”,并在数据库中注册插件模型,等等。但我需要一种方法来处理每个插件的urlpattern。目前,我已经在webapp的“核心”中编写了一个函数,用于注册一个模型,并(理论上)将上传的插件的core模式添加到webapp的urls.py中。此函数为:

代码语言:javascript
复制
def register_plugin_model(model,codename):
# Standard syncdb expects models to be in reliable locations,
# so dynamic models need to bypass django.core.management.syncdb.
# On the plus side, this allows individual models to be installed
# without installing the entire project structure.
# On the other hand, this means that things like relationships and
# indexes will have to be handled manually.
# This installs only the basic table definition.

if model is not None:
    style = color.no_style()
    cursor = connection.cursor()
    tables = connection.introspection.table_names()
    seen_models = connection.introspection.installed_models(tables)
    statements,trsh = connection.creation.sql_create_model(model, style, seen_models)
    for sql in statements:
        cursor.execute(sql)

# add urlpatterns
from django.conf.urls.defaults import patterns, url,include
from project.plugins.urls import urlpatterns
urlpatterns += patterns(url(r'^' + codename + '/' , include ( 'media.plugins.' + codename + '.urls' )))

插件以tgz格式上传到media/tmp,然后解压到media/ Plugins /,其中是插件的代号,用户上传的插件由project.plugins管理。

所有的插件逻辑都工作得很好,但是当我尝试将上传的插件urls.py文件包含到webapp (project.plugins.urls)中时,它不起作用。我打印了"project.plugins.urls.urlpatterns“的值,在”urlpattern += pat...“之后没有修改。

有什么办法可以满足我的需求吗?

诚挚的问候

EN

回答 2

Stack Overflow用户

发布于 2012-03-30 06:47:13

您面临的问题是,在项目url.py文件中定义的urlpatterns和在register_plugin文件中定义的urlpatterns是不同的变量。它们对于模块来说是本地的。想象一下下面的场景:

代码语言:javascript
复制
#math.py
pi = 3.14

#some_nasty_module.py
from math import pi
pi = 'for_teh_luls'

#your_module.py
from math import pi
pi * 2
>>> 'for_teh_lulsfor_teh_luls'
# wtf?

很明显你不被允许这么做。您可能需要做的是,让原始的urls.py尝试发现您的插件文件夹中的urls。

代码语言:javascript
复制
# urls.py
urlpatterns += (...)

def load_plugin_urls():
    for module in get_plugin_modules():
        try:
            from module.urls import urlpatterns as u
            urlpatterns += u
        except:
            pass

不幸的是,for服务器将需要回收该进程才能运行此代码,因此只有在发生这种情况时,上传插件才会生效。

票数 4
EN

Stack Overflow用户

发布于 2012-03-30 18:13:57

修改urls.py和urls.py的函数属于同一个模块。我通过添加一个“空模式”解决了这个问题:

代码语言:javascript
复制
urlpatterns += patterns('',url(r'^' + codename + '/' , include ( 'media.plugins.' + codename + '.urls' )))

现在,它说:

代码语言:javascript
复制
BEFORE:
<RegexURLPattern plugins ^$>
<RegexURLPattern plugin_new ^new/$>
<RegexURLPattern plugin_profile ^profile/(?P<plugin>\w+)$>
AFTER
<RegexURLPattern plugins ^$>
<RegexURLPattern plugin_new ^new/$>
<RegexURLPattern plugin_profile ^profile/(?P<plugin>\w+)$>
<RegexURLResolver media.plugins.sampleplugin.urls (None:None) ^sampleplugin/>

但是你说过,它不会立即生效:/

我在没有删除*.pyc文件的情况下重新启动了应用程序,但更改没有生效。有什么问题吗?

PD:插件urls.py文件包含:

代码语言:javascript
复制
from django.conf.urls.defaults import patterns, url
from .views import index_view

urlpatterns = patterns( '' , url(r'^.*' , index_view ) )

感谢您的回复

诚挚的问候

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9934573

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档