我不确定为什么我会在下面收到此错误。我遵循了我认为正确的语法。
错误消息:
AttributeError的 AttributeError:' CampaignView'对象没有属性' object'
完整跟踪:
Traceback (most recent call last):
File "/Users/user/Documents/workspace/test/django-env/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 67, in __call__
return self.application(environ, start_response)
File "/Users/user/Documents/workspace/testsite/django-env/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 206, in __call__
response = self.get_response(request)
File "/Users/user/Documents/workspace/testsite/django-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 194, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/Users/user/Documents/workspace/testsite/django-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 229, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/Users/user/Documents/workspace/testsite/django-env/lib/python2.7/site-packages/django_extensions/management/technical_response.py", line 5, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/Users/user/Documents/workspace/testsite/django-env/lib/python2.7/site-packages/django/core/handlers/base.py", line 112, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/user/Documents/workspace/testsite/django-env/lib/python2.7/site-packages/django/views/generic/base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/user/Documents/workspace/testsite/django-env/lib/python2.7/site-packages/django/views/generic/base.py", line 87, in dispatch
return handler(request, *args, **kwargs)
File "/Users/user/Documents/workspace/testsite/testsite/apps/campaigns/views/campaign.py", line 54, in post
return super(CampaignView, self).form_invalid(form)
File "/Users/user/Documents/workspace/testsite/django-env/lib/python2.7/site-packages/django/views/generic/edit.py", line 85, in form_invalid
return self.render_to_response(self.get_context_data(form=form))
File "/Users/user/Documents/workspace/testsite/django-env/lib/python2.7/site-packages/django/views/generic/detail.py", line 96, in get_context_data
if self.object:
AttributeError: 'CampaignView' object has no attribute 'object'
查看
class CampaignView(UpdateView):
form_class = UserLookUpFrom
model = Campaign
success_url = reverse_lazy('campaigns_detail')
def form_valid(self, form):
"""
If the form is valid, in this case DON'T save it!
"""
return super(CampaignView, self).form_valid(form)
def _user_exists(self, email_mobile):
"""
Checks if user exists from email.
:return: true or false : boolean
"""
return AppUser.objects.does_user_exists(email_mobile)
def _get_user_id(self, email_mobile):
"""
Returns user ID based on email.
"""
return AppUser.objects.get_user_id(email_mobile)
def post(self, request, *args, **kwargs):
"""
Handles POST requests, instantiating a form instance with the passed
POST variables and then checked for validity.
"""
form = self.form_class(request.POST)
if form.is_valid():
email_mobile = form.cleaned_data['email_mobile']
if self._user_exists(email_mobile):
user_id = self._get_user_id(email_mobile)
return redirect(reverse('campaigns_share', args=(self.object.id, user_id)))
return super(CampaignView, self).form_valid(form)
return super(CampaignView, self).form_invalid(form)
答案 0 :(得分:2)
你已经覆盖了post
方法,因此很多调用POST的标准设置都不会被调用。
你所拥有的大多数代码 - is_valid()
块中的所有代码 - 在form_valid
方法中会更好。其余部分由标准行为涵盖,因此您可以完全删除post
方法。