将模型从 url 传递到 create_object 通用输入视图

发布于 2024-08-23 16:33:45 字数 520 浏览 9 评论 0原文

我有多个模型想要为其创建通用输入。我的第一遍使用了两个单独的 url:

url(r'^create_actor/$, create_object, {'model': Actor, 'template_name': 'create.html', 'post_save_redirect': '/library/', 'extra_context': {'func': 'Create Actor'}, 'login_required': 'True'}),

url(r'^create_movie/$, create_object, {'model': Movie, 'template_name': 'create.html', 'post_save_redirect': '/library/', 'extra_context': {'func': 'Create Movie'}, 'login_required': 'True'}),

我认为将它们合并到一个语句中会更好。我不确定如何将变量从 url 传递到参数中,以便该行根据该变量动态选择模型。

I have multiple models that I want to create generic inputs for. My first pass used two separate urls:

url(r'^create_actor/$, create_object, {'model': Actor, 'template_name': 'create.html', 'post_save_redirect': '/library/', 'extra_context': {'func': 'Create Actor'}, 'login_required': 'True'}),

url(r'^create_movie/$, create_object, {'model': Movie, 'template_name': 'create.html', 'post_save_redirect': '/library/', 'extra_context': {'func': 'Create Movie'}, 'login_required': 'True'}),

I assume it would be much better to combine these into one statement. I'm not sure how to pass a variable from the url into the parameters such that the line would dynamically select the model based on the variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心不设防 2024-08-30 16:33:45

我还没有尝试过这个,但是您可以使用变量来捕获 create_ 之后的值并将其自动发送到 create_object 视图:

url(r'url(r'^create_(?P<model>\w+)/$, create_object, {'template_name': 'create.html', 'post_save_redirect': '/library/', 'login_required': 'True'})

您必须以 /create_Actor/ 而不是 /creat_actor/ 的形式访问此 url。我不知道如何让 extra_context 键起作用。
希望这有帮助。

I haven't tried this, but you can use a variable to capture the value after create_ and have it automatically sent to the create_object view:

url(r'url(r'^create_(?P<model>\w+)/$, create_object, {'template_name': 'create.html', 'post_save_redirect': '/library/', 'login_required': 'True'})

You'll have to access this url as /create_Actor/ instead of /creat_actor/. I'm not sure how to get the extra_context key to work.
Hope this helps.

画骨成沙 2024-08-30 16:33:45
from django.db import models

url(r'^create_(?P<modelname>\w+)/
, generic_add),

def generic_add(request, modelname):
    mdlnm_model = models.get_model('catalog',modelname)
    return create_object(request,
       model = mdlnm_model,
       template_name = 'create.html',
       post_save_redirect = '/library/',
       login_required = 'True'
    )
from django.db import models

url(r'^create_(?P<modelname>\w+)/
, generic_add),

def generic_add(request, modelname):
    mdlnm_model = models.get_model('catalog',modelname)
    return create_object(request,
       model = mdlnm_model,
       template_name = 'create.html',
       post_save_redirect = '/library/',
       login_required = 'True'
    )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文