我试图获取当前正在编辑的记录的id,但目前尚未成功。我的视图如下:
views.py
class EditSite(UpdateView):
model = SiteData
form_class = SiteForm
template_name = "sites/site_form.html"
@method_decorator(user_passes_test(lambda u: u.has_perm('config.edit_subnet')))
def dispatch(self, *args, **kwargs):
self.site_id = self.object.pk
return super(EditSite, self).dispatch(*args, **kwargs)
def get_success_url(self, **kwargs):
return reverse_lazy("sites:site_overview", args=(self.site_id,))
def get_form_kwargs(self, *args, **kwargs):
kwargs = super().get_form_kwargs()
return kwargs
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['SiteID']=self.site_id
context['SiteName']=self.location
context['FormType']='Edit'
return context
并且出现了错误:
File "/itapp/itapp/sites/views.py" in dispatch
890. self.site_id = self.object.pk
Exception Type: AttributeError at /sites/site/edit/7
Exception Value: 'EditSite' object has no attribute 'object'
我已经尝试过:
self.object.pk
object.pk
self.pk
get_object().id
代替object.id
。 - mohammedgqudah