7

(実行時に) 動的モジュールをロードする必要がある Django アプリケーションを開発しています。これで、(クライアント ブラウザーからサーバーに) "プラグイン" をアップロードし、プラグイン モデルをデータベースに登録することができます。ただし、各プラグインの urlpatterns を処理する方法が必要です。現在、モデルを登録し、(理論的には) アップロードされたプラグインの urlpatterns を webapp urls.py に追加する関数を webapp の「コア」に記述しました。この関数は次のとおりです。

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」の値を出力しましたが、「urlpatterns += pat....」の後に変更されていません。

必要なことを行う方法はありますか?

よろしくお願いします

4

2 に答える 2

4

あなたが直面している問題はurlpatterns、プロジェクトurl.pyファイルでurlpatterns定義されているものと、ファイルで定義されてregister_pluginいるものが異なる変数であることです。それらはモジュールに対してローカルです。次のシナリオを想像してください。

#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にあなたのプラグインフォルダ内の URL を発見するように依頼することです.

# 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

残念ながら、ウェブサーバーはこのコードを実行するためにプロセスをリサイクルする必要があるため、プラグインのアップロードはそれが発生して初めて有効になります。

于 2012-03-29T22:47:12.637 に答える
3

urls.py を変更する関数と urls.py は同じモジュールに属します。「空のパターン」を追加して解決しました:

urlpatterns += patterns('',url(r'^' + codename + '/' , include ( 'media.plugins.' + codename + '.urls' )))

今、それは言います:

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 ファイルには以下が含まれます。

from django.conf.urls.defaults import patterns, url
from .views import index_view

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

お返事をありがとうございます

よろしくお願いします

于 2012-03-30T10:13:56.933 に答える