我正在编辑现有项目,我是 django 的新手。这是写在 url 文件中的
url(r'^test/$', views.MyUpdate.as_view(model=models.User,
form_class=forms.UserForm), name='user_update'),
现在我想知道如何在我的视图中访问这些变量
您可以使用获取表单,self.get_form().__class__
并且应该能够使用 获取模型self.get_object().__class__
。
显然要获取表单实例或模型实例,只需使用self.get_form()
or self.get_object()
。
如果您希望通过 url 将参数传递给视图,您可以将其self.args[0]
用于位置参数和self.kwargs['my_kwarg']
关键字参数。
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
基本上你正在覆盖这些属性,因此它们会自动填充