How to pass parameters to django generic views

做~自己de王妃 提交于 2019-12-04 21:17:18

You have missed the entire point of generic views. For a simple DetailView - which is a view of a single object - you just define the model and slug attributes in the class:

(r'^newreportview/(\d+)/$', NewReportView.as_view()),  

class NewReportView(DetailView):
    template_name = "report/newreportview.html"
    model = MyTask
    slug = 'applicationnnumber'

(You could also just as easily have passed those three as parameters in the URL definition, so no need to make a subclass at all.)

The reason why you were getting no values for self.args is that you had passed your parameter as a kwarg, not an arg. So self.kwargs['number'] would have worked, as would the revised URL I've shown here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!