I'm overriding the init method in my form andthis is now returning an error 'TransactionForm' object has no attribute '_errors'.
I would expect this to work because I've included super in my init, however perhaps I don't understand how to use this correctly. An explanation would be appreciated. What do I need to do to get form.errors working?
Full traceback
Traceback:
File "C:\ProgramFiles\Python36\lib\site-packages\django\core\handlers\exception.py" ininner35. response = get_response(request)
File "C:\ProgramFiles\Python36\lib\site-packages\django\core\handlers\base.py" in_get_response128. response = self.process_exception_by_middleware(e, request)
File "C:\ProgramFiles\Python36\lib\site-packages\django\core\handlers\base.py" in_get_response126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\py\portfolio-project\myportfolio\views.py" in add_transaction136. return render(request, 'myportfolio/add_transaction.html', {'form': form})
File "C:\Program Files\Python36\lib\site-packages\django\shortcuts.py"in render36. content = loader.render_to_string(template_name, context, request, using=using)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\loader.py" inrender_to_string62. return template.render(context, request)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\backends\django.py"in render61. return self.template.render(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\base.py" in render175. return self._render(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\base.py" in _render167. return self.nodelist.render(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\base.py" in render943. bit = node.render_annotated(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\base.py" inrender_annotated910. return self.render(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\loader_tags.py" inrender155. return compiled_parent._render(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\base.py" in _render167. return self.nodelist.render(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\base.py" in render943. bit = node.render_annotated(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\base.py" inrender_annotated910. return self.render(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\loader_tags.py" inrender155. return compiled_parent._render(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\base.py" in _render167. return self.nodelist.render(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\base.py" in render943. bit = node.render_annotated(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\base.py" inrender_annotated910. return self.render(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\loader_tags.py" inrender67. result = block.nodelist.render(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\base.py" in render943. bit = node.render_annotated(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\base.py" inrender_annotated910. return self.render(context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\base.py" in render999. return render_value_in_context(output, context)
File "C:\ProgramFiles\Python36\lib\site-packages\django\template\base.py" inrender_value_in_context978. value = str(value)
File "C:\ProgramFiles\Python36\lib\site-packages\django\utils\html.py" in 371. klass.str = lambda self: mark_safe(klass_str(self))
File "C:\ProgramFiles\Python36\lib\site-packages\django\forms\forms.py" in str136. return self.as_table()
File "C:\ProgramFiles\Python36\lib\site-packages\django\forms\forms.py" in as_table279. errors_on_separate_row=False)
File "C:\ProgramFiles\Python36\lib\site-packages\django\forms\forms.py" in_html_output196. top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
File "C:\ProgramFiles\Python36\lib\site-packages\django\forms\forms.py" innon_field_errors305. return self.errors.get(NON_FIELD_ERRORS, self.error_class(error_class='nonfield'))
File "C:\ProgramFiles\Python36\lib\site-packages\django\forms\forms.py" in errors173. if self._errors is None:
Exception Type: AttributeError at /myportfolio/add_transaction/Exception Value: 'TransactionForm' object has no attribute '_errors'
forms
class TransactionForm(forms.ModelForm): CHOICES = ((1, 'Buy'), (2, 'Sell'),)coin = forms.ModelChoiceField(queryset = Coin.objects.all()) buysell = forms.ChoiceField(choices = CHOICES)field_order = ['buysell', 'coin', 'amount', 'trade_price']class Meta:model = Transactionsfields = {'buysell', 'coin', 'amount', 'trade_price'}def __init__(self, coin_price = None, user = None, *args, **kwargs):if user:self.user = userqs_coin = Portfolio.objects.filter(user = self.user).values('coin').distinct()super(TransactionForm, self).__init__(self.user, *args, **kwargs)self.fields['coin'].queryset = qs_coinif coin_price:self.coin_price = coin_pricesuper(TransactionForm, self).__init__(self.user, self.coin_price, *args, **kwargs)self.fields['price'] = self.coin_price
Views
def add_transaction(request):print(request.method)print("test1")print(request.GET)if request.method == "GET":if request.is_ajax():print("ajax test")data = {'test': "test1"}form = TransactionForm(request.GET, user = request.user, coin_price = GetCoin("Bitcoin").price)return JsonResponse(data)form = TransactionForm()if request.method == "POST":print("test2")form = TransactionForm(request.POST)if form.is_valid():print("test3")obj = form.save(commit = False)obj.user = request.userobj.save()return HttpResponseRedirect('/myportfolio/')else: print(form.errors)return render(request, 'myportfolio/add_transaction.html', {'form': form})