zoukankan      html  css  js  c++  java
  • django 高级

    1.使用form:

      django的form提供了统一的结构化的后台验证机制,错误信息,也容易展现在前台界面上。由于python的面向对象,使得编写html也能够代码复用。

      a.多个field 综合验证,需要重写clean 程序

    def clean(self):
    """Checks that no two articles have the same title."""
      if any(self.errors):
      # Don’t bother validating the formset unless each form is valid on its own
        return
      titles = []
      for form in self.forms:
        title = form.cleaned_data[’title’]
      if title in titles:
        raise forms.ValidationError("Articles in a set must have distinct titles.")
      titles.append(title)

      b.template中form 的要素:

         form :

    {{form.as_p}}, {{form.as_table}} ,{{form.as_url}}

          {{form.hidden_fields}} 隐藏的字段

          {{form.visible_fields}} 显示的字段

               {{form.media}}: 样式与js定义文件

          {{form.non_field_errors}}:非字段相关的错误

        字段 a:{{form.a}}  

          字段样式定制,在python中:这种方式适合继承,比如有好几个页面都需要使用该form,这种方式适合。

    domain_name=forms.CharField(widget=TextInput(attrs={"class":"form-control","size":"16","placeholder":"输入您要加速的域名。如:image.a.com"}))

          或者在html中

    <input class="form-control" size="16" name="{{ form.domain_name.html_name }}" type="text" value="" placeholder="输入您要加速的域名。如:image.a.com">

          字段的属性

            {{form.a.label}} 标签名字

            {{form.a.label_tag}} 标签的html tag形式

            {{form.a.value}} 值

            {{form.a.html_name}} input 的name

            {{form.a.help_text}} input 的帮助

            {{form.a.errors}} input 的错误

            {{form.a.is_hidden}} input 是否隐藏

            {{form.a.field}} a字段的各个属性字典   

    2.widget:定制化form的字段的表现形式。

    class CalendarWidget(forms.TextInput):
        class Media:
            css = {
                ’all’: (’pretty.css’,)
            }
            js = (’animations.js’, ’actions.js’)
    
    class CalendarWidget(forms.TextInput):
        def _media(self):
            return forms.Media(css={’all’: (’pretty.css’,)},js=(’animations.js’, ’actions.js’))
        media = property(_media)        

          

         

      

  • 相关阅读:
    NOIP2009 pj
    数星星(POJ2352 star)
    洛谷 p3372 模板-线段树 1
    Luogu P1198 [JSOI2008]最大数 线段树
    Bestcoder#92&HDU 6017 T3 Girl loves 233 DP
    NOIP2008pj & luoguP1058 立体图 模拟
    NOIP2003TG 加分二叉树 区间DP
    Redis Jedis lua脚本
    Mac Ideal 常用快捷键
    Mysql慢查询explain
  • 原文地址:https://www.cnblogs.com/yasmi/p/5563790.html
Copyright © 2011-2022 走看看