1

我正在尝试在我的 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') 
4

2 回答 2

2

您必须在表单中进行验证错误;

class AddUserAccountsForm(forms.Form):

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request', None)
        super(AddUserAccountsForm, self).__init__(*args, **kwargs)


    def clean_name(self):
        Institution = Institution.objects.get(name=self.cleaned_data['name'])
        new_entry = User.objects.get(id=self.request.user.id)
        if Account.objects.filter(user=new_entry, institution=Institution.type).exists():
            raise forms.ValidationError(_('This account already exists'), code='invalid')
        return self.cleaned_data['name']

在视图中;

form = AddUserAccountsForm(request.POST, request=request)
于 2015-11-26T05:34:16.340 回答
0

@GeoJacob 谢谢。为了解决用户 ID 问题,我使用了以下代码:

def clean_name(self):
    Institution = Institution_long_tail.objects.get(name=self.cleaned_data['name'])
    current_user = wellfyUser.objects.get(id=self.instance.id)
    if Access_token_pair.objects.filter(user=current_user, institution=Institution.type).exists():
        raise forms.ValidationError(_('This account already exists'), code='invalid') 
    return self.cleaned_data['name']

有观点:

form = AddUserAccountsForm(request.POST, instance=current_user)
于 2015-11-26T06:56:10.077 回答