简体   繁体   English

如何将参数传递给django中基于类的视图?

[英]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 我的urlconf看起来像

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

I want to pass parameters to the urlconf the old functional way 我想以旧的功能方式将参数传递给urlconf

(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. 如何将参数传递给我的urlconf以及如何在类视图中接收参数。

They are passed in the old way. 他们在旧的方式通过。

You access them via self.args and self.kwargs , for positional and keyword arguments respectively. 您可以通过self.argsself.kwargs分别访问位置和关键字参数。 In your case, self.kwargs['id'] would do the trick. 在你的情况下, self.kwargs['id']可以解决问题。

Edit because you've overridden get() but not preserved the signature. 编辑,因为您已覆盖get()但未保留签名。 If you're overriding a method, always do def get(self, request, *args, **kwargs) . 如果你要覆盖一个方法,总是做def get(self, request, *args, **kwargs)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django 基于类的视图:如何将附加参数传递给 as_view 方法? - Django class-based view: How do I pass additional parameters to the as_view method? 如何使用Django 1.8从基于类的View将参数传递给表单? - How to pass parameters to a form from a class based View with Django 1.8? 如何从Django中另一个基于类的视图中返回基于类的视图的结果? - How do I return the result of a class-based view from another class-based view in Django? 如何向基于Django类的通用视图装饰器添加参数? - How to add parameters to Django class based generic view decorators? 如何将基于 class 的视图中的 pk 参数传递给 Django 中的查询集 - How to pass pk argument within class based view to queryset in Django Django-如何通过基于类的视图将对象传递给模板? - Django - how to pass object to a template with a class based view? 如何在基于 Class 的视图的上下文中传递给 Django 中的模板? - How to pass in the context of a Class Based View to the Template in Django? Django反向url与参数到基于类的视图 - Django reverse url with parameters to a class based view 我正在尝试将 celery 应用于 django 中基于 Class 的视图(apis)。 我怎样才能做到这一点? - I'm trying to apply celery to Class based view(apis) in django. How can I do this? 如何在 django 中通过 url 传递参数? - How do I pass parameters via url in django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM