如何验证表单并显示填充字段的值?

发布于 2024-07-30 20:25:39 字数 1426 浏览 8 评论 0原文

现在我正在学习验证表单,“全部”正在工作,我显示空字段的错误,但我有两个问题:

  1. 当其他字段中存在错误时,如何显示填充字段中的值?,例如 < ;input ... value= {{ value }} > 问题是我的字段不是 html 表单字段。
  2. 如何在空白字段上准确显示错误?

我怎么有这个:

form.py

class NuevaDiligenciaForm(forms.Form):
    titulo = forms.CharField(max_length=70)    
    tipo = forms.ChoiceField(choices=TIPO)        
    vias= forms.TypedChoiceField(widget=forms.RadioSelect(), choices=CHOICES)

view.py

def add(request):        
    form = NuevaDiligenciaForm()
    errors =[]
    if request.method =='POST': 
        if not request.POST.get('titulo',''):
            errors.append('Titulo es requerido')
        if not request.POST.get('vias',''):
            errors.append('Vias es requerido')
        #if not errors:
    return render_to_response('account/add.html', { 'formulario':form ,'errors':errors},context_instance = RequestContext(request))   

template.html

{% if errors %}
    <ul>
        {% for error in errors %}
        <li>{{ error }}</li>
        {% endfor %}
    </ul>
{% endif %}

{% if message %}
        <p style="color: red;">{{ message }}</p>
{% endif %}
<form action='.' method='POST' class="nueva-diligencia"> 

{{ formulario.as_p }}

<input type="submit" value="Continuar">
</form>

再次感谢:)

now im learning to validate form, "all" is working, im showing the erros of empty fields, but i have 2 questions:

  1. how ill show the value in the filled fields when there are errors in another fields?, like <input ... value= {{ value }} > the problem is that my fields are not html forms fields.
  2. how ill show the error exactly over the empty fields?

how i have this:

form.py

class NuevaDiligenciaForm(forms.Form):
    titulo = forms.CharField(max_length=70)    
    tipo = forms.ChoiceField(choices=TIPO)        
    vias= forms.TypedChoiceField(widget=forms.RadioSelect(), choices=CHOICES)

view.py

def add(request):        
    form = NuevaDiligenciaForm()
    errors =[]
    if request.method =='POST': 
        if not request.POST.get('titulo',''):
            errors.append('Titulo es requerido')
        if not request.POST.get('vias',''):
            errors.append('Vias es requerido')
        #if not errors:
    return render_to_response('account/add.html', { 'formulario':form ,'errors':errors},context_instance = RequestContext(request))   

template.html

{% if errors %}
    <ul>
        {% for error in errors %}
        <li>{{ error }}</li>
        {% endfor %}
    </ul>
{% endif %}

{% if message %}
        <p style="color: red;">{{ message }}</p>
{% endif %}
<form action='.' method='POST' class="nueva-diligencia"> 

{{ formulario.as_p }}

<input type="submit" value="Continuar">
</form>

Thanks again :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟酉 2024-08-06 20:25:42

您的表单代码在这里看起来不错,但您的视图需要更改为:

def add(request):
if request.method =='POST':
    form = NuevaDiligenciaForm(request.POST)
    if form.is_valid():
        clean_data = form.cleaned_data
        # Now do something with the cleaned data...
else:
    form = NuevaDiligenciaForm()
return render_to_response('account/add.html', { 'formulario': form }

并且您的模板应如下所示:

{% if message %}
<p style="color: red;">{{ message }}</p>
{% endif %}
<form action='.' method='POST' class="nueva-diligencia"> 
    {{ formulario.as_p }}
    <input type="submit" value="Continuar">
</form>

现在发生的情况是,如果 POST 中存在错误数据, form.is_valid() 将失败,视图将返回经过验证的表单,其中包含包含错误的字段旁边的错误。 Django 在这里为您处理所有错误! 尝试一下,让我知道它是否如您所期望的那样工作。

如果您想了解这个简化版本如何/为什么实际上效果更好,这是一个非常好的资源: http://www.djangobook.com/en/2.0/chapter07/

You form code looks fine here but your view needs to change to this:

def add(request):
if request.method =='POST':
    form = NuevaDiligenciaForm(request.POST)
    if form.is_valid():
        clean_data = form.cleaned_data
        # Now do something with the cleaned data...
else:
    form = NuevaDiligenciaForm()
return render_to_response('account/add.html', { 'formulario': form }

and your template should look like this:

{% if message %}
<p style="color: red;">{{ message }}</p>
{% endif %}
<form action='.' method='POST' class="nueva-diligencia"> 
    {{ formulario.as_p }}
    <input type="submit" value="Continuar">
</form>

Now what happens is that if there is bad data from the POST, form.is_valid() will fail and the view will return the validated form, which will include errors next to fields that have them. Django takes care of all the error handling for you here! Try it out and let me know if it works as you expect it to.

This is a pretty good resource if you'd like to see how/why this simplified version actually works better: http://www.djangobook.com/en/2.0/chapter07/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文