我正在尝试在我的 html 中输出一条错误消息。我尝试使用其他问题的建议,但我仍然在回溯屏幕上收到错误,而不是在 html 上。有人可以帮忙吗?
这是我的观点:
def add_user_institution_accounts(request):
context_object_name = 'variable_used_in `add_user_accounts.html`'
form = AddUserAccountsForm
template_name = 'add_user_accounts.html'
if request.method == 'POST':
# create a form instance and populate it with data from the request:
form = AddUserAccountsForm(request.POST)
# check whether it's valid:
if form.is_valid():
Institution = Institution.objects.get(name=form.cleaned_data['name'])
new_entry = User.objects.get(id=request.user.id)
if Account.objects.filter(user=new_entry, institution=Institution.type).exists():
raise forms.ValidationError(_('This account already exists'), code='invalid')
else:
Account.objects.create(user=new_entry, institution = Institution.type)
return HttpResponseRedirect('')
else:
messages.error(request, "Error")
return render(request, 'add_user_accounts.html',
{'form': form},
{'form_errors': form_errors},
context_instance=RequestContext(request))
我的html是:
{% extends "registration/registration_base.html" %}
{% load i18n %}
{% block content %}
{% load staticfiles %}
<script type="text/javascript" src="{% static 'jquery.js' %}"></script>
{% include 'autocomplete_light/static.html' %}
<form action="" method="post">
{% csrf_token %}
{{ form.as_p }}
{{ form.errors }}
<input type="submit" value="Add account" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
{% endblock %}
这是回溯错误:
ValidationError at /profile/add_user_accounts/
[u'This account already exists']
发生在这条线上:
raise forms.ValidationError(_('This account already exists'), code='invalid')