Reputation: 31548
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'
Upvotes: 17
Views: 38508
Reputation: 11
This is how our url
should look like:
url(regex, view, kwargs=None, name=None, prefix='')
The main part here is that this kwargs
argument should be a dictionary. This is the problem with your code.
So, instead of passing model=models.userModel
, you should pass it in the form of a dictionary in your url
like this:
kwargs=dict(model=models.userModel)
Because if you look carefully in your views.py
code, you will notice that you were trying to put kwargs['model']
(the value stored in the kwargs
dictionary with the model
key) into the model
variable.
class myview(TemplateView):
def myfunction(self,request, object_id, **kwargs):
model = kwargs['model']
But since you were not passing any dictionary, you were not able to access the kwargs
dictionary.
Upvotes: 1
Reputation: 33420
This:
url(r'^download/template/(?P<object_id>\d+)/$', views.myview().myfunction,model=models.userModel, name="sample")
Should be:
url(r'^download/template/(?P<object_id>\d+)/$', views.myview.as_view(model=models.userModel), name="sample")
See docs
Your current implementation is not thread safe. For example:
from django import http
from django.contrib.auth.models import User
from django.views import generic
class YourView(generic.TemplateView):
def __init__(self):
self.foo = None
def your_func(self, request, object_id, **kwargs):
print 'Foo', self.foo
self.foo = 'bar'
return http.HttpResponse('foo')
urlpatterns = patterns('test_app.views',
url(r'^download/template/(?P<object_id>\d+)/$', YourView().your_func,
kwargs=dict(model=User), name="sample"),
)
Would you expect it to print 'Foo None' ? Well be careful cause the instance is shared across requests:
Django version 1.4.2, using settings 'django_test.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Foo None
[03/Dec/2012 08:14:31] "GET /test_app/download/template/3/ HTTP/1.1" 200 3
Foo bar
[03/Dec/2012 08:14:32] "GET /test_app/download/template/3/ HTTP/1.1" 200 3
So, when it's not thread safe, you can't assume that it will be in a clean state when the request starts - unlike when using as_view().
Upvotes: 8
Reputation: 6769
I believe you would have the same functionality (and avoid threading issues) if you did this in your views.py
from django.views.generic import TemplateView
from .models import userModel
class myview(TemplateView):
def myfunction(self, request, object_id, *args, **kwargs):
model = userModel
# ... Do something with it
Upvotes: 0
Reputation: 1121864
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")
Upvotes: 20