Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何处理view.py文件中的django模型表单错误?_Python_Django Forms_Django Views_Django Templates - Fatal编程技术网

Python 如何处理view.py文件中的django模型表单错误?

Python 如何处理view.py文件中的django模型表单错误?,python,django-forms,django-views,django-templates,Python,Django Forms,Django Views,Django Templates,这是我的forms.py文件。我们可以从forms.py中的clean方法验证表单数据 from django import forms from django.core.exceptions import ValidationError class ContactForm(forms.Form): # Everything as before. ... def clean_recipients(self):

这是我的forms.py文件。我们可以从forms.py中的clean方法验证表单数据

from django import forms
from django.core.exceptions import ValidationError


    class ContactForm(forms.Form):
        # Everything as before.
        ...
    
        def clean_recipients(self):
            data = self.cleaned_data['recipients']
            if "fred@example.com" not in data:
                raise ValidationError("You have forgotten about Fred!")
    
            # Always return a value to use as the new cleaned data, even if
            # this method didn't change it.
但是我想验证view.py中的一些数据。我们可以这样做吗?在view.py文件中

class Test(View) :
    def get(self,request):
      #Get logic 
   def post(self,request):
      form = ContactForm(request.POST or None)
      if form.is_valid():
          num = form.cleaned_data['num']
          
          if '123' in num:
               form.add_error('num','Not valid')
          else:
               form.save()
              
      return render(request,self.template_name,{'form':form})
使用消息处理view.py而不是forms.py中的表单错误的方法是什么

from django import forms
from django.core.exceptions import ValidationError


    class ContactForm(forms.Form):
        # Everything as before.
        ...
    
        def clean_recipients(self):
            data = self.cleaned_data['recipients']
            if "fred@example.com" not in data:
                raise ValidationError("You have forgotten about Fred!")
    
            # Always return a value to use as the new cleaned data, even if
            # this method didn't change it.
  • views.py

    from django.contrib import messages
    class Test(View) :
        def post(self,request):
            form = ContactForm(request.POST or None)
            if form.is_valid():
                num = form.cleaned_data['num']
    
                if '123' in num:
                    messages.error(request, 'Not valid')
                else:
                    form.save()
    
            return render(request,self.template_name,{'form':form})
    
  • 模板

    {% if messages %}
        {% for message in messages %}
            <p>{{ message }}</p>
        {% endfor %}
    {% endif %}
    
    {{num}}
    
  • 然后在模板中使用变量

    {{num}}
    

  • 您可以使用消息,也可以将表单错误作为上下文传递给模板。