Search code examples
pythondjango

How can I pass kwargs in URL in django


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'

Solution

  • 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")