gpt4 book ai didi

jquery - Django - clean() 具有隐藏形式

转载 作者:行者123 更新时间:2023-12-01 03:25:31 24 4
gpt4 key购买 nike

我在这里遇到问题,我的 def clean() 无法使用我的隐藏 form

我有三个表单,其中我使用jquery通过select隐藏它,例如,如果我想要 form1 只是我必须选择 form1 它会隐藏其他 2。

现在我正在尝试使用clean(),但是当我收到raise ValidationError时,我看不到它,因为我的form被隐藏。

所以我需要点击form1,然后我可以看到我的错误

有没有可能的方法来查看我的错误,即使它是隐藏的?

因为有时我不知道为什么会收到错误,直到我点击 form1

我一直在寻找可能的解决方案,但一无所获。

模板

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.1.min.js"></script>
{{ titulo }}
<hr/>
<br/>

{% if messages %}
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
{% for message in messages %}
{% if message.tags %}<div class="alert alert-{{ message.tags }}">{{ message }}</div>{% endif %}
{% endfor %}
</div>
{% endif %}

<center>
<label for="protocolo">Protocolo de Activos</label>
<select id="protocolo" name="protocolo">
{% for x,y in form.fields.protocolo.choices %}
<option value="{{ x }}"{% if form.fields.protocolo.value == x %} selected{% endif %}>{{ y }}</option>
{% endfor %}
</select>
</center>
<br>

<div id="form1" style="display:none;">
<form method="POST" action="">{% csrf_token %}
{{ form1.as_p }}
<input type="submit" value="Enviar Datos" />
</form>
</div>

<script>
$('#protocolo').on('change',function(){
var selection = $(this).val();
switch(selection){
case "form1":
$("#form1").show()
break;
default:
$("#form1").hide()
}
});
</script>

如您所见,我正在使用 jqueryshow()hide() form1

如果name存在,此代码工作正常,我会收到错误,但当我单击select时,我会看到该消息并选择 form1

我想在点击提交时看到它...可能吗?

注意:

这次我只有一个 form 作为 form1,但我很快就会添加更多,这就是我使用 select 的原因> 与 Jquery

谢谢!!

编辑:

forms.py

class userForm(forms.ModelForm):
class Meta:
model = user
fields = ["name"]

def clean(self, *args, **kwargs):
try:
name = self.cleaned_data.get('name')
name,created = user.get_or_create(name=name)
except Exception, e:
raise ValidationError('Error, already exists')
return super(userForm, self).clean(*args, **kwargs)

Views.py

def user_form(request):
titulo = "Activos"
form1 = userForm(request.POST or None)
queryset = user.objects.all()
context = {
"form1": form,
"queryset": queryset,
}
if form1.is_valid():
instance = form.save()
messages.success(request, 'Has been added')
return redirect('/')
return render(request, "user.html", context)

我刚刚使用 viewsform 编辑了我的代码。

一旦我转到我的表单并尝试注册已存在的用户,我就会收到错误消息,没关系。但是我之前解释过一个 Jquery 效果,它阻止我看到错误消息,除非我单击我的选择并选择我的表单,然后我可以看到我的标志错误 错误,已存在

一旦我点击提交并重定向我,我希望看到错误,已经存在

clean() 方法仅在我选择 form

时有效

最佳答案

答案是你不需要在这里重写 clean 方法 because

The ModelForm.clean() method sets a flag that makes the model validation step validate the uniqueness of model fields that are marked as unique, unique_together or unique_for_date|month|year.

If you would like to override the clean() method and maintain this validation, you must call the parent class’s clean() method.

所以你所需要的只是表单.py

class userForm(forms.ModelForm):
class Meta:
model = user
fields = ["name"]

就是这样!

关于jquery - Django - clean() 具有隐藏形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41420845/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com