通过 urls.py 将字典参数传递给视图时出现 TypeError
我正在尝试使用字典将关键字参数传递给 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哎呀。 有点尴尬,但我在处理该函数时复制/粘贴了该函数,并且没有重命名原始函数。 现在正在按预期工作...
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...