1

django中只使用ModleForm的表单验证,而不使用ModleForm来渲染 - 野生大虾

 1 year ago
source link: https://www.cnblogs.com/mooremok/p/17003453.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

  博客园 ::

首页 :: 博问 :: 闪存 ::

新随笔 ::

联系 ::

订阅

::

管理 ::

  8 随笔 :: 10 文章 :: 0 评论 ::

189 阅读

众所周知,django.forms极其强大,不少的框架也借鉴了这个模式,如Scrapy。在表单验证时,django.forms是一绝,也是面向对象的经典表现。但要用它来渲染表单那就不好玩了,除非写框架。本文章主要缕一缕如何使用django.forms来做表单验证。

django项目基本信息

  • models.py
from django.db import models


class Article(models.Model):
    title = models.CharField(max_length=50, verbose_name='标题')
    content = models.TextField(verbose_name='内容')
    create_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title
  • forms.py
    同目录下创建一个forms.py
from django.forms import ModelForm
from django import forms
from . models import Article
from django.core.exceptions import ValidationError

class ArticleForm(ModelForm):
    class Meta:
        model = Article
        exclude = ['id']
    
    def clean(self):
        cleaned_data = super().clean()
        title = cleaned_data.get('title')
        if 'My' not in title:
            raise ValidationError('标题中必须包含My字样', code='title')
  • views.py
    在views中,创建一个增加Article的方法
def add(request):
    if request.method == 'GET':
        return render(request, 'add.html')
    else:
        form = ArticleForm(request.POST)
        # 主要了解的是表单的验证
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/show/')
        else:
            form.errors.as_data()            # {'__all__': [ValidationError(['标题中必须包含My字样'])]}
            form.errors.get_context()        # {'errors': dict_items([('__all__', ['标题中必须包含My字样'])]), 'error_class': 'errorlist'}
            d = form.errors.get_json_data()  # {'__all__': [{'message': '标题中必须包含My字样', 'code': 'title'}]}
            return HttpResponse(d.get('__all__'))

如果是在admin中使用ModelForm的验证,那也是非常方便的,如果我们要在用户的前端响应中使用表单验证,且又不通过django.forms渲染的表单来传递验证结果,则需要看看源码:
ModelForm.errorserrorsErrorDict()的实例,ErrorDict源码:

class ErrorDict(dict, RenderableErrorMixin):
    """
    A collection of errors that knows how to display itself in various formats.

    The dictionary keys are the field names, and the values are the errors.
    """

    template_name = "django/forms/errors/dict/default.html"
    template_name_text = "django/forms/errors/dict/text.txt"
    template_name_ul = "django/forms/errors/dict/ul.html"

    def __init__(self, *args, renderer=None, **kwargs):
        super().__init__(*args, **kwargs)
        self.renderer = renderer or get_default_renderer()

    def as_data(self):
        return {f: e.as_data() for f, e in self.items()}

    def get_json_data(self, escape_html=False):
        return {f: e.get_json_data(escape_html) for f, e in self.items()}

    def get_context(self):
        return {
            "errors": self.items(),
            "error_class": "errorlist",
        }

三个方法返回的都是字典,但数据结构不同,可以看情况而定。值得注意的是,在ArticleForm中,raise ValidationError时,如果code传入参数时,它将会在get_context()中显式体现出来。

1、掌握这个原理,传统的全栈开发可以节省更多的时间。
2、多看源码


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK