简体   繁体   中英

How do i pass parameters to a class based view in django?

I have the following class based view;

class myClassView():
    def get(self):
        # lots of code ...
        return response

My urlconf for this looks like

(r^'call_myClassView/', myClassView.as_view())

I want to pass parameters to the urlconf the old functional way

(r'call_myClassView/(?P<id>\w+)/$',myClassView.as_view())

How do i pass parameters to my urlconf and how do i receive the parameter in my class view.

They are passed in the old way.

You access them via self.args and self.kwargs , for positional and keyword arguments respectively. In your case, self.kwargs['id'] would do the trick.

Edit because you've overridden get() but not preserved the signature. If you're overriding a method, always do def get(self, request, *args, **kwargs) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM