Django class-based view: How do I pass additional parameters to the as_view method?

后端 未结 7 851
悲哀的现实
悲哀的现实 2020-11-28 19:00

I have a custom class-based view

# myapp/views.py
from django.views.generic import *

class MyView(DetailView):
    template_name = \'detail.html\'
    model         


        
7条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 19:54

    For django 3.0, this is what worked for me:

    # myapp/views.py
    from django.views.generic import DetailView
    
    class MyView(DetailView):
        template_name = 'detail.html'
        slug = None
    
        def get_object(self, queryset=None):
            self.slug = self.kwargs.get('slug', None)
            return queryset.get(slug=self.slug)
    
    # myapp/urls.py
    from django.urls import path
    from . import views
    
    urlpatterns = [
        path('slug//', views.MyView.as_view(), name='myview_by_tag'),
    ]
    

提交回复
热议问题