我正在编辑现有项目,我是django的新手。 这是用url文件写的
url(r'^test/$', views.MyUpdate.as_view(model=models.User,
form_class=forms.UserForm), name='user_update'),
现在我想知道如何在我的视图中访问这些变量
答案 0 :(得分:0)
您可以使用self.get_form().__class__
获取表单,并且您应该能够使用self.get_object().__class__
获取模型。
显然,只需使用self.get_form()
或self.get_object()
来获取表单实例或模型实例。
如果你想通过url传递给视图的参数,你可以使用self.args[0]
作为位置参数,使用self.kwargs['my_kwarg']
作为关键字参数。
答案 1 :(得分:0)
https://docs.djangoproject.com/en/dev/topics/class-based-views/
Any arguments passed to as_view() will override attributes set on the class.
UpdateView的类属性
http://ccbv.co.uk/projects/Django/1.4/django.views.generic.edit/UpdateView/
form_class = None
http_method_names = ['get', 'post', 'put', 'delete', 'head', 'options', 'trace']
initial = {}
model = None
基本上你要覆盖这些属性,因此它们会自动填充