1
所以我一直在嘗試添加一個取消按鈕到我一直在使用的UpdateView。這裏是我的代碼吧:「UpdateView」對象沒有屬性「對象」
views.py:
class CountryEditView(generic.edit.UpdateView):
model = Country
fields = ['name']
template_name_suffix = '_edit'
def post(self, request, *args, **kwargs):
if "cancel" in request.POST:
url = self.object.get_absolute_url()
return HttpResponseRedirect(url)
else:
return super(CountryEditView, self).post(request, *args, **kwargs)
# etc....
models.py:
class Country(AutoUpdateModel): #A subclass of models.Model
def get_absolute_url(self):
return reverse('appName:country_info', args=(self.id,))
#etc...
country_edit.html:
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Update" />
<input type="submit" name="cancel" value="Cancel" />
</form>
但是我已經得到這個錯誤:
Traceback (most recent call last):
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\exception.py", line 42, in inner
response = get_response(request)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\generic\base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\username\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\username\Envs\projName\appName\views.py", line 38, in post
url = self.object.get_success_url()
AttributeError: 'CountryEditView' object has no attribute 'object'
這對我沒有意義,因爲documentation page說When using UpdateView you have access to self.object, which is the object being updated.
有什麼我做錯了嗎?
爲什麼定義兩次'POST'? – user2361174
呵呵 - 不知道這是怎麼發生的 - 編輯它 –