通过 urls.py 将字典参数传递给视图时出现 TypeError

发布于 2024-08-01 16:33:39 字数 813 浏览 5 评论 0原文

我正在尝试使用字典将关键字参数传递给 Django 视图,但是当我尝试访问 URL 时,我不断遇到 TypeError(错误是:“add_business_contact() 获得了意外的关键字参数 'info_models'”)。 代码是:

urlpatterns = patterns('business.views',
    # ...
    url(r'^(?P<business_id>[\w\._-]+)/edit_contact$', 'add_business_contact', {
        'info_models': [Email, PhoneNumber, URL] }, name='business_contact'),
    # ...
)

以及相应的视图:

@login_required
def add_business_contact(request, business_id, *args, **kwargs):
    # ...
    info_models = kwargs.pop('info_models', None)
    # ....

如果我从 url() 函数中删除字典参数,它会很高兴地到达并运行视图(尽管不正确,因为它没有该参数)。 关于为什么要这样做有什么想法吗? 我正在遵循 Django 书中的一个示例 ( http://djangobook.com/en/2.0/ Chapter08/ )如果有帮助的话。

I'm trying to pass keyword arguments to a Django view using a dictionary, but I keep running into a TypeError when I try to access the URL (The error is: "add_business_contact() got an unexpected keyword argument 'info_models'"). The code is:

urlpatterns = patterns('business.views',
    # ...
    url(r'^(?P<business_id>[\w\._-]+)/edit_contact

and the corresponding view:

@login_required
def add_business_contact(request, business_id, *args, **kwargs):
    # ...
    info_models = kwargs.pop('info_models', None)
    # ....

If I remove the dictionary argument from the url() function, it happily reaches and runs the view (albeit incorrectly since it doesn't have that argument). Any ideas as to why it's doing this? I'm following an example from the Django Book ( http://djangobook.com/en/2.0/chapter08/ ) if that helps at all.

, 'add_business_contact', { 'info_models': [Email, PhoneNumber, URL] }, name='business_contact'), # ... )

and the corresponding view:

If I remove the dictionary argument from the url() function, it happily reaches and runs the view (albeit incorrectly since it doesn't have that argument). Any ideas as to why it's doing this? I'm following an example from the Django Book ( http://djangobook.com/en/2.0/chapter08/ ) if that helps at all.

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

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

发布评论

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

评论(1

高速公鹿 2024-08-08 16:33:39

哎呀。 有点尴尬,但我在处理该函数时复制/粘贴了该函数,并且没有重命名原始函数。 现在正在按预期工作...

Wooops. A bit embarassing but I copy/pasted the function while working on it and didn't rename the original. It's working now as expected...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文