zoukankan      html  css  js  c++  java
  • django中的Form

    基于django.forms.Form:所有表单类的父类
    基于django.forms.ModelForm:可以和模型绑定的form
     
    djangoform的最简单的实现:
    在Form类中添加
     
    class PublisherForm(forms.Form):
    name=forms.CharField(label="名称",error_messages={"required":"不能为空"})
    address=forms.CharField("label"="地址")
    city=forms.CharField()
    state_province=forms.CharField()
    country=forms.CharField()
    website=forms.URLField()
     
    label为展现在html中所呈现的标签的显示名
     
    在views中添加
    Publisher_Form=PublisherForm()
     
    在html中
    <form method="POST" action="">
    {{Publisher_Form.as_p}}
    </form>
     
    在view中获取数据
    form_p=PublisherForm(request.POST) 初始化提交的内容
    if publisher_form.is_valid(): 验证数据
    publisher.objects.create(
    name=form_p.cleaned_data['name'] 获取表单中的值
    )
    return HttpResponse('ok')
     
     
    使用ModeForm生成表单
    from models import publisher
    class PublisherForm(forms.ModelForm):
    class Meta:
    model=publisher //绑定model
    exclude={"id",} //除id外都显示
     
    使用ModelForm接收并添加数据
     
    form_p=PublisherForm(request.POST) 初始化提交的内容
    if publisher_form.is_valid(): 验证数据
    form_p.save()
    return HttpResponse('ok')
     
     
  • 相关阅读:
    HDU
    HDU-1166 敌兵布阵 (基础线段树)
    Matrices with XOR property (暴力)
    CF-825E Minimal Labels (反向拓扑)
    CodeForces-1144E Median String (模拟)
    操作文件和目录
    文件读写
    装饰器
    数据结构和算法
    Scrapy shell调试返回403错误
  • 原文地址:https://www.cnblogs.com/lijintian/p/6100099.html
Copyright © 2011-2022 走看看