如何在django的URL中传递kwargs [英] How can I pass kwargs in URL in django

查看:200
本文介绍了如何在django的URL中传递kwargs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在django文档中,url函数就是这样

  url(regex,view,kwargs = None,name = None ,prefix ='')

我有这个

  url(r'^ download / template /(?P< object_id> \d +)/ $',views.myview()。myfunction,model = models.userModel,name =sample)

这是我的看法



$ my $($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ model model model model model model model model model model model model model model model model model model model model model model model model model model model model ']

我收到这个错误

  url()得到一个意想不到的关键字参数'model'


解决方案

您正尝试将模型关键字参数传递给 url()功能;你需要传递一个 kwargs 参数(它需要一个字典):

  url(r'^ download / template /(?P< object_id> \d +)/ $',views.myview()。myfunction,
kwargs = dict(model = models.userModel),name =sample)


In the django doc the url function is like this

url(regex, view, kwargs=None, name=None, prefix='')

I have this

url(r'^download/template/(?P<object_id>\d+)/$', views.myview().myfunction,model=models.userModel, name="sample")

This is my view

class myview(TemplateView):

    def myfunction(self,request, object_id, **kwargs):
        model = kwargs['model']

I get this error

url() got an unexpected keyword argument 'model'

解决方案

You are trying to pass in a model keyword argument to the url() function; you need to pass in a kwargs argument instead (it takes a dictionary):

url(r'^download/template/(?P<object_id>\d+)/$', views.myview().myfunction, 
    kwargs=dict(model=models.userModel), name="sample")

这篇关于如何在django的URL中传递kwargs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆