1. 程式人生 > >django form 表單

django form 表單

   
  <!DOCTYPE html>
  <html lang="zh-cn">
  <head>
  <meta charset="utf-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="referrer" content="origin" />
  <title>Django Form和ModelForm元件 - Q1mi - 部落格園</title>
  <meta property="og:description" content="Form介紹 我們之前在HTML頁面中利用form表單向後端提交資料時,都會寫一些獲取使用者輸入的標籤並且用form標籤把它們包起來。 與此同時我們在好多場景下都需要對使用者的輸入做校驗,比如校驗使用者是否" />
  <link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=-duj5vpGTntb85GJoM3iRI972XwWcI-j8zmqDzyfu2w1"/>
  <link id="MainCss" type="text/css" rel="stylesheet" href="/skins/SimpleMemory/bundle-SimpleMemory.css?v=EanP3quXYv9G0oFCmz4BNBHvmK7M_Si1iD1EpUHL_441
"/>
  <link type="text/css" rel="stylesheet" href="/blog/customcss/260599.css?v=onf6r%2feJ3O3eaiWY9gfxVD5ElDg%3d"/>
  <link id="mobile-style" media="only screen and (max-width: 767px)" type="text/css" rel="stylesheet" href="/skins/SimpleMemory/bundle-SimpleMemory-mobile.css?v=XGOE8_i3XCsAIQHcgl5c_8VAMMKRkj0W3Eckyc8qbso1"/>
  <link title="RSS" type="application/rss+xml" rel="alternate" href="https://www.cnblogs.com/liwenzhou/rss"/>
  <link title="RSD" type="application/rsd+xml" rel="EditURI" href="https://www.cnblogs.com/liwenzhou/rsd.xml"/>
  <link type="application/wlwmanifest+xml" rel="wlwmanifest" href="https://www.cnblogs.com/liwenzhou/wlwmanifest.xml"/>
  <script src="//common.cnblogs.com/scripts/jquery-2.2.0.min.js"></script>
  <script type="text/javascript">var currentBlogApp = 'liwenzhou', cb_enable_mathjax=false;var isLogined=false;</script>
  <script src="/bundles/blog-common.js?v=_U8OpbfIPk4K3Mxrx3oPrbJt2JRnf47OVRruaRlQuEc1" type="text/javascript"></script>
  </head>
  <body>
  <a name="top"></a>
  <!--PageBeginHtml Block Begin-->
  <link type="text/css" rel="stylesheet" href="https://blog-static.cnblogs.com/files/liwenzhou/myblog.css">
  <!--PageBeginHtml Block End-->
   
  <!--done-->
  <div id="home">
  <div id="header">
  <div id="blogTitle">
  <a id="lnkBlogLogo" href="https://www.cnblogs.com/liwenzhou/"><img id="blogLogo" src="/Skins/custom/images/logo.gif" alt="返回主頁" /></a>
   
  <!--done-->
  <h1><a id="Header1_HeaderTitle" class="headermaintitle" href="https://www.cnblogs.com/liwenzhou/">Q1mi</a></h1>
  <h2></h2>
   
   
   
   
  </div><!--end: blogTitle 部落格的標題和副標題 -->
  <div id="navigator">
   
  <ul id="navList">
  <li><a id="blog_nav_sitehome" class="menu" href="https://www.cnblogs.com/">部落格園</a></li>
  <li><a id="blog_nav_myhome" class="menu" href="https://www.cnblogs.com/liwenzhou/">首頁</a></li>
  <li><a id="blog_nav_newpost" class="menu" rel="nofollow" href="https://i.cnblogs.com/EditPosts.aspx?opt=1">新隨筆</a></li>
  <li><a id="blog_nav_contact" class="menu" rel="nofollow" href="https://msg.cnblogs.com/send/Q1mi">聯絡</a></li>
  <li><a id="blog_nav_rss" class="menu" href="https://www.cnblogs.com/liwenzhou/rss">訂閱</a>
  <!--<a id="blog_nav_rss_image" class="aHeaderXML" href="https://www.cnblogs.com/liwenzhou/rss"><img src="//www.cnblogs.com/images/xml.gif" alt="訂閱" /></a>--></li>
  <li><a id="blog_nav_admin" class="menu" rel="nofollow" href="https://i.cnblogs.com/">管理</a></li>
  </ul>
  <div class="blogStats">
   
  <div id="blog_stats">
  <span id="stats_post_count">隨筆 - 54&nbsp; </span>
  <span id="stats_article_count">文章 - 20&nbsp; </span>
  <span id="stats-comment_count">評論 - 7</span>
  </div>
   
  </div><!--end: blogStats -->
  </div><!--end: navigator 部落格導航欄 -->
  </div><!--end: header 頭部 -->
   
  <div id="main">
  <div id="mainContent">
  <div class="forFlow">
   
  <div id="post_detail">
  <!--done-->
  <div id="topics">
  <div class = "post">
  <h1 class = "postTitle">
  <a id="cb_post_title_url" class="postTitle2" href="https://www.cnblogs.com/liwenzhou/p/8747872.html">Django Form和ModelForm元件</a>
  </h1>
  <div class="clear"></div>
  <div class="postBody">
  <div id="cnblogs_post_body" class="blogpost-body"><h1>Form介紹&nbsp;</h1>
  <p>我們之前在HTML頁面中利用form表單向後端提交資料時,都會寫一些獲取使用者輸入的標籤並且用form標籤把它們包起來。</p>
  <p>與此同時我們在好多場景下都需要對使用者的輸入做校驗,比如校驗使用者是否輸入,輸入的長度和格式等正不正確。如果使用者輸入的內容有錯誤就需要在頁面上相應的位置顯示對應的錯誤資訊.。</p>
  <p>Django form元件就實現了上面所述的功能。</p>
  <p>總結一下,其實form元件的主要功能如下:</p>
  <ul>
  <li>生成頁面可用的HTML標籤</li>
  <li>對使用者提交的資料進行校驗</li>
  <li>保留上次輸入內容</li>
  </ul>
  <h2>普通方式手寫註冊功能</h2>
  <h3>views.py</h3>
  <div class="cnblogs_code">
  <pre><span style="color: #008000;">#</span><span style="color: #008000;"> 註冊</span>
  <span style="color: #0000ff;">def</span><span style="color: #000000;"> register(request):
  error_msg </span>= <span style="color: #800000;">""</span>
  <span style="color: #0000ff;">if</span> request.method == <span style="color: #800000;">"</span><span style="color: #800000;">POST</span><span style="color: #800000;">"</span><span style="color: #000000;">:
  username </span>= request.POST.get(<span style="color: #800000;">"</span><span style="color: #800000;">name</span><span style="color: #800000;">"</span><span style="color: #000000;">)
  pwd </span>= request.POST.get(<span style="color: #800000;">"</span><span style="color: #800000;">pwd</span><span style="color: #800000;">"</span><span style="color: #000000;">)
  </span><span style="color: #008000;">#</span><span style="color: #008000;"> 對註冊資訊做校驗</span>
  <span style="color: #0000ff;">if</span> len(username) &lt; 6<span style="color: #000000;">:
  </span><span style="color: #008000;">#</span><span style="color: #008000;"> 使用者長度小於6位</span>
  error_msg = <span style="color: #800000;">"</span><span style="color: #800000;">使用者名稱長度不能小於6位</span><span style="color: #800000;">"</span>
  <span style="color: #0000ff;">else</span><span style="color: #000000;">:
  </span><span style="color: #008000;">#</span><span style="color: #008000;"> 將使用者名稱和密碼存到資料庫</span>
  <span style="color: #0000ff;">return</span> HttpResponse(<span style="color: #800000;">"</span><span style="color: #800000;">註冊成功</span><span style="color: #800000;">"</span><span style="color: #000000;">)
  </span><span style="color: #0000ff;">return</span> render(request, <span style="color: #800000;">"</span><span style="color: #800000;">register.html</span><span style="color: #800000;">"</span>, {<span style="color: #800000;">"</span><span style="color: #800000;">error_msg</span><span style="color: #800000;">"</span>: error_msg})</pre>
  </div>
  <h3>login.html</h3>
  <div class="cnblogs_code">
  <pre><span style="color: #0000ff;">&lt;!</span><span style="color: #ff00ff;">DOCTYPE html</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">html </span><span style="color: #ff0000;">lang</span><span style="color: #0000ff;">="en"</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">head</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">meta </span><span style="color: #ff0000;">charset</span><span style="color: #0000ff;">="UTF-8"</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">title</span><span style="color: #0000ff;">&gt;</span>註冊頁面<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">title</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">head</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">body</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">form </span><span style="color: #ff0000;">action</span><span style="color: #0000ff;">="/reg/"</span><span style="color: #ff0000;"> method</span><span style="color: #0000ff;">="post"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
  {% csrf_token %}
  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
  使用者名稱:
  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="name"</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
  密碼:
  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="password"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="pwd"</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="註冊"</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="color: red"</span><span style="color: #0000ff;">&gt;</span>{{ error_msg }}<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">form</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">body</span><span style="color: #0000ff;">&gt;</span>
  <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">html</span><span style="color: #0000ff;">&gt;</span></pre>
  </div>
  <h2>使用form元件實現註冊功能</h2>
  <h3>views.py</h3>
  <p>先定義好一個RegForm類:</p>
  <div class="cnblogs_code">
  <pre><span style="color: #0000ff;">from</span> django <span style="color: #0000ff;">import</span><span style="color: #000000;"> forms
   
  </span><span style="color: #008000;">#</span><span style="color: #008000;"> 按照Django form元件的要求自己寫一個類</span>
  <span style="color: #0000ff;">class</span><span style="color: #000000;"> RegForm(forms.Form):
  name </span>= forms.CharField(label=<span style="color: #800000;">"</span><span style="color: #800000;">使用者名稱</span><span style="color: #800000;">"</span><span style="color: #000000;">)
  pwd </span>= forms.CharField(label=<span style="color: #800000;">"</span><span style="color: #800000;">密碼</span><span style="color: #800000;">"</span>)</pre>
  </div>
  <p>再寫一個檢視函式:</p>
  <div class="cnblogs_code">
  <pre><span style="color: #008000;">#</span><span style="color: #008000;"> 使用form元件實現註冊方式</span>
  <span style="color: #0000ff;">def</span><span