當前位置: 首頁>>代碼示例>>Golang>>正文


Golang wtforms.NewForm函數代碼示例

本文整理匯總了Golang中github.com/jimmykuu/wtforms.NewForm函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewForm函數的具體用法?Golang NewForm怎麽用?Golang NewForm使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewForm函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: adminNewAdHandler

// URL: /admin/ad/new
// 添加廣告
func adminNewAdHandler(handler *Handler) {
	defer dps.Persist()

	choices := []wtforms.Choice{
		wtforms.Choice{"top0", "最頂部"},
		wtforms.Choice{"top", "頂部"},
		wtforms.Choice{"frontpage", "首頁"},
		wtforms.Choice{"content", "主題內"},
		wtforms.Choice{"2cols", "2列寬度"},
		wtforms.Choice{"3cols", "3列寬度"},
		wtforms.Choice{"4cols", "4列寬度"},
	}
	form := wtforms.NewForm(
		wtforms.NewSelectField("position", "位置", choices, "", wtforms.Required{}),
		wtforms.NewTextField("name", "名稱", "", wtforms.Required{}),
		wtforms.NewTextField("index", "序號", "", wtforms.Required{}),
		wtforms.NewTextArea("code", "代碼", "", wtforms.Required{}),
	)

	if handler.Request.Method == "POST" {
		if !form.Validate(handler.Request) {
			handler.renderTemplate("ad/form.html", ADMIN, map[string]interface{}{
				"form":  form,
				"isNew": true,
			})
			return
		}

		c := handler.DB.C(ADS)
		index, err := strconv.Atoi(form.Value("index"))
		if err != nil {
			form.AddError("index", "請輸入正確的數字")
			handler.renderTemplate("ad/form.html", ADMIN, map[string]interface{}{
				"form":  form,
				"isNew": true,
			})
			return
		}

		err = c.Insert(&AD{
			Id_:      bson.NewObjectId(),
			Position: form.Value("position"),
			Name:     form.Value("name"),
			Code:     form.Value("code"),
			Index:    index,
		})

		if err != nil {
			panic(err)
		}

		http.Redirect(handler.ResponseWriter, handler.Request, "/admin/ads", http.StatusFound)
		return
	}

	handler.renderTemplate("ad/form.html", ADMIN, map[string]interface{}{
		"form":  form,
		"isNew": true,
	})
}
開發者ID:ZuiGuangYin,項目名稱:gopher,代碼行數:62,代碼來源:ad.go

示例2: newArticleHandler

// URL: /article/new
// 新建文章
func newArticleHandler(w http.ResponseWriter, r *http.Request) {
	var categories []ArticleCategory
	c := DB.C("articlecategories")
	c.Find(nil).All(&categories)

	var choices []wtforms.Choice

	for _, category := range categories {
		choices = append(choices, wtforms.Choice{Value: category.Id_.Hex(), Label: category.Name})
	}

	form := wtforms.NewForm(
		wtforms.NewHiddenField("html", ""),
		wtforms.NewTextField("title", "標題", "", wtforms.Required{}),
		wtforms.NewTextField("original_source", "原始出處", "", wtforms.Required{}),
		wtforms.NewTextField("original_url", "原始鏈接", "", wtforms.URL{}),
		wtforms.NewSelectField("category", "分類", choices, ""),
	)

	if r.Method == "POST" && form.Validate(r) {
		user, _ := currentUser(r)

		c = DB.C("contents")

		id_ := bson.NewObjectId()

		html := form.Value("html")
		html = strings.Replace(html, "<pre>", `<pre class="prettyprint linenums">`, -1)

		categoryId := bson.ObjectIdHex(form.Value("category"))
		err := c.Insert(&Article{
			Content: Content{
				Id_:       id_,
				Type:      TypeArticle,
				Title:     form.Value("title"),
				CreatedBy: user.Id_,
				CreatedAt: time.Now(),
			},
			Id_:            id_,
			CategoryId:     categoryId,
			OriginalSource: form.Value("original_source"),
			OriginalUrl:    form.Value("original_url"),
		})

		if err != nil {
			fmt.Println("newArticleHandler:", err.Error())
			return
		}

		http.Redirect(w, r, "/a/"+id_.Hex(), http.StatusFound)
		return
	}

	renderTemplate(w, r, "article/form.html", map[string]interface{}{
		"form":   form,
		"title":  "新建",
		"action": "/article/new",
		"active": "article",
	})
}
開發者ID:jemygraw,項目名稱:gopher,代碼行數:62,代碼來源:article.go

示例3: adminEditPackageCategoryHandler

// URL: /admin/package_category/{id}/edit
// 修改包分類
func adminEditPackageCategoryHandler(w http.ResponseWriter, r *http.Request) {
	id := mux.Vars(r)["id"]
	c := DB.C(PACKAGE_CATEGORIES)
	var category PackageCategory
	c.Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&category)

	form := wtforms.NewForm(
		wtforms.NewTextField("id", "ID", category.Id, wtforms.Required{}),
		wtforms.NewTextField("name", "名稱", category.Name, wtforms.Required{}),
	)

	if r.Method == "POST" {
		if !form.Validate(r) {
			renderTemplate(w, r, "package_category/form.html", ADMIN, map[string]interface{}{"form": form})
			return
		}

		c.Update(bson.M{"_id": bson.ObjectIdHex(id)}, bson.M{"$set": bson.M{
			"id":   form.Value("id"),
			"name": form.Value("name"),
		}})

		http.Redirect(w, r, "/admin/package_categories", http.StatusFound)
	}

	renderTemplate(w, r, "package_category/form.html", ADMIN, map[string]interface{}{
		"form":  form,
		"isNew": false,
	})
}
開發者ID:nicai1900,項目名稱:gopher,代碼行數:32,代碼來源:package_category.go

示例4: profileHandler

// URL /profile
// 用戶設置頁麵,顯示用戶設置,用戶頭像,密碼修改
func profileHandler(w http.ResponseWriter, r *http.Request) {
	user, ok := currentUser(r)

	if !ok {
		http.Redirect(w, r, "/signin?next=/profile", http.StatusFound)
		return
	}

	profileForm := wtforms.NewForm(
		wtforms.NewTextField("email", "電子郵件", user.Email, wtforms.Email{}),
		wtforms.NewTextField("website", "個人網站", user.Website),
		wtforms.NewTextField("location", "所在地", user.Location),
		wtforms.NewTextField("tagline", "簽名", user.Tagline),
		wtforms.NewTextArea("bio", "個人簡介", user.Bio),
	)

	if r.Method == "POST" {
		if profileForm.Validate(r) {
			c := db.C("users")
			c.Update(bson.M{"_id": user.Id_}, bson.M{"$set": bson.M{"website": profileForm.Value("website"),
				"location": profileForm.Value("location"),
				"tagline":  profileForm.Value("tagline"),
				"bio":      profileForm.Value("bio"),
			}})
			http.Redirect(w, r, "/profile", http.StatusFound)
			return
		}
	}

	renderTemplate(w, r, "account/profile.html", map[string]interface{}{"user": user, "profileForm": profileForm})
}
開發者ID:RaymondChou,項目名稱:gopher_blog,代碼行數:33,代碼來源:account.go

示例5: adminNewSiteCategoryHandler

// URL: /admin/site_category/new
// 新建站點分類
func adminNewSiteCategoryHandler(w http.ResponseWriter, r *http.Request) {
	form := wtforms.NewForm(
		wtforms.NewTextField("name", "名稱", "", wtforms.Required{}),
	)

	if r.Method == "POST" {
		if !form.Validate(r) {
			renderTemplate(w, r, "admin/new_site_category.html", map[string]interface{}{"adminNav": ADMIN_NAV, "form": form})
			return
		}

		c := DB.C("sitecategories")
		var category SiteCategory
		err := c.Find(bson.M{"name": form.Value("name")}).One(&category)

		if err == nil {
			form.AddError("name", "該名稱已經有了")
			renderTemplate(w, r, "admin/new_site_category.html", map[string]interface{}{"adminNav": ADMIN_NAV, "form": form})
			return
		}

		err = c.Insert(&SiteCategory{
			Id_:  bson.NewObjectId(),
			Name: form.Value("name"),
		})

		if err != nil {
			panic(err)
		}

		http.Redirect(w, r, "/admin/site_category/new", http.StatusFound)
	}

	renderTemplate(w, r, "admin/new_site_category.html", map[string]interface{}{"adminNav": ADMIN_NAV, "form": form})
}
開發者ID:jinzhe,項目名稱:gopher,代碼行數:37,代碼來源:admin.go

示例6: profileHandler

// URL /profile
// 用戶設置頁麵,顯示用戶設置,用戶頭像,密碼修改
func profileHandler(w http.ResponseWriter, r *http.Request) {
	user, _ := currentUser(r)

	profileForm := wtforms.NewForm(
		wtforms.NewTextField("email", "電子郵件", user.Email, wtforms.Email{}),
		wtforms.NewTextField("website", "個人網站", user.Website),
		wtforms.NewTextField("location", "所在地", user.Location),
		wtforms.NewTextField("tagline", "簽名", user.Tagline),
		wtforms.NewTextArea("bio", "個人簡介", user.Bio),
		wtforms.NewTextField("github_username", "GitHub用戶名", user.GitHubUsername),
		wtforms.NewTextField("weibo", "新浪微博", user.Weibo),
	)

	if r.Method == "POST" {
		if profileForm.Validate(r) {
			c := DB.C(USERS)
			c.Update(bson.M{"_id": user.Id_}, bson.M{"$set": bson.M{
				"website":        profileForm.Value("website"),
				"location":       profileForm.Value("location"),
				"tagline":        profileForm.Value("tagline"),
				"bio":            profileForm.Value("bio"),
				"githubusername": profileForm.Value("github_username"),
				"weibo":          profileForm.Value("weibo"),
			}})
			http.Redirect(w, r, "/profile", http.StatusFound)
			return
		}
	}

	renderTemplate(w, r, "account/profile.html", BASE, map[string]interface{}{
		"user":           user,
		"profileForm":    profileForm,
		"defaultAvatars": defaultAvatars,
	})
}
開發者ID:nicai1900,項目名稱:gopher,代碼行數:37,代碼來源:account.go

示例7: loginHandler

// URL: /login
// 處理用戶登錄,如果登錄成功,設置Cookie
func loginHandler(handler Handler) {

	form := wtforms.NewForm(
		wtforms.NewTextField("username", "用戶名", "", &wtforms.Required{}),
		wtforms.NewPasswordField("password", "密碼", &wtforms.Required{}),
	)

	if handler.Request.Method == "POST" {
		if form.Validate(handler.Request) {

			if form.Value("username") == "sll" &&
				form.Value("password") == "123456" {
			} else {
				form.AddError("password", "密碼和用戶名不匹配")

				renderHtml(handler, "login.html", map[string]interface{}{"form": form})
				return
			}

			session, _ := store.Get(handler.Request, "user")
			session.Values["username"] = form.Value("username")
			session.Save(handler.Request, handler.ResponseWriter)

			http.Redirect(handler.ResponseWriter, handler.Request, "/", http.StatusFound)

			return
		}
	}

	renderHtml(handler, "login.html", map[string]interface{}{"form": form})
}
開發者ID:mysll,項目名稱:flynet,代碼行數:33,代碼來源:admin.go

示例8: adminNewArticleCategoryHandler

// URL: /admin/article_category/new
// 新建文章分類
func adminNewArticleCategoryHandler(handler Handler) {
	form := wtforms.NewForm(
		wtforms.NewTextField("name", "名稱", "", wtforms.Required{}),
	)

	if handler.Request.Method == "POST" {
		if !form.Validate(handler.Request) {
			renderTemplate(handler, "article_category/new.html", ADMIN, map[string]interface{}{"form": form})
			return
		}

		c := handler.DB.C(ARTICLE_CATEGORIES)
		var category ArticleCategory
		err := c.Find(bson.M{"name": form.Value("name")}).One(&category)

		if err == nil {
			form.AddError("name", "該名稱已經有了")
			renderTemplate(handler, "article_category/new.html", ADMIN, map[string]interface{}{"form": form})
			return
		}

		err = c.Insert(&ArticleCategory{
			Id_:  bson.NewObjectId(),
			Name: form.Value("name"),
		})

		if err != nil {
			panic(err)
		}

		http.Redirect(handler.ResponseWriter, handler.Request, "/admin/article_category/new", http.StatusFound)
	}

	renderTemplate(handler, "article_category/new.html", ADMIN, map[string]interface{}{"form": form})
}
開發者ID:hello-kukoo,項目名稱:gopher,代碼行數:37,代碼來源:article_category.go

示例9: changePasswordHandler

// URL: /user_center/change_password
// 修改密碼
func changePasswordHandler(handler *Handler) {
	user, _ := currentUser(handler)

	form := wtforms.NewForm(
		wtforms.NewPasswordField("current_password", "當前密碼", wtforms.Required{}),
		wtforms.NewPasswordField("new_password", "新密碼", wtforms.Required{}),
		wtforms.NewPasswordField("confirm_password", "新密碼確認", wtforms.Required{}),
	)

	if handler.Request.Method == "POST" && form.Validate(handler.Request) {
		if form.Value("new_password") == form.Value("confirm_password") {
			if user.CheckPassword(form.Value("current_password")) {
				c := handler.DB.C(USERS)
				salt := strings.Replace(uuid.NewUUID().String(), "-", "", -1)
				c.Update(bson.M{"_id": user.Id_}, bson.M{"$set": bson.M{
					"password": encryptPassword(form.Value("new_password"), salt),
					"salt":     salt,
				}})
				message(handler, "密碼修改成功", `密碼修改成功`, "success")
				return
			} else {
				form.AddError("current_password", "當前密碼錯誤")
			}
		} else {
			form.AddError("confirm_password", "密碼不匹配")
		}
	}

	handler.renderTemplate("user_center/change_password.html", BASE, map[string]interface{}{
		"form":   form,
		"active": "change_password",
	})
}
開發者ID:ZuiGuangYin,項目名稱:gopher,代碼行數:35,代碼來源:user_center.go

示例10: changePasswordHandler

// URL: /change_password
// 修改密碼
func changePasswordHandler(handler Handler) {
	user, _ := currentUser(handler)

	form := wtforms.NewForm(
		wtforms.NewPasswordField("current_password", "當前密碼", wtforms.Required{}),
		wtforms.NewPasswordField("new_password", "新密碼", wtforms.Required{}),
		wtforms.NewPasswordField("confirm_password", "新密碼確認", wtforms.Required{}),
	)

	if handler.Request.Method == "POST" && form.Validate(handler.Request) {
		if form.Value("new_password") == form.Value("confirm_password") {
			currentPassword := encryptPassword(form.Value("current_password"))
			if currentPassword == user.Password {
				c := handler.DB.C(USERS)
				c.Update(bson.M{"_id": user.Id_}, bson.M{"$set": bson.M{"password": encryptPassword(form.Value("new_password"))}})
				message(handler, "密碼修改成功", `密碼修改成功`, "success")
				return
			} else {
				form.AddError("current_password", "當前密碼錯誤")
			}
		} else {
			form.AddError("confirm_password", "密碼不匹配")
		}
	}

	renderTemplate(handler, "account/change_password.html", BASE, map[string]interface{}{"form": form})
}
開發者ID:hello-kukoo,項目名稱:gopher,代碼行數:29,代碼來源:account.go

示例11: changePasswordHandler

// URL: /change_password
// 修改密碼
func changePasswordHandler(w http.ResponseWriter, r *http.Request) {
	user, ok := currentUser(r)

	if !ok {
		http.Redirect(w, r, "/signin?next=/change_password", http.StatusFound)
		return
	}

	form := wtforms.NewForm(
		wtforms.NewPasswordField("current_password", "當前密碼", wtforms.Required{}),
		wtforms.NewPasswordField("new_password", "新密碼", wtforms.Required{}),
		wtforms.NewPasswordField("confirm_password", "新密碼確認", wtforms.Required{}),
	)

	if r.Method == "POST" && form.Validate(r) {
		if form.Value("new_password") == form.Value("confirm_password") {
			currentPassword := encryptPassword(form.Value("current_password"))
			if currentPassword == user.Password {
				c := DB.C("users")
				c.Update(bson.M{"_id": user.Id_}, bson.M{"$set": bson.M{"password": encryptPassword(form.Value("new_password"))}})
				message(w, r, "密碼修改成功", `密碼修改成功`, "success")
				return
			} else {
				form.AddError("current_password", "當前密碼錯誤")
			}
		} else {
			form.AddError("confirm_password", "密碼不匹配")
		}
	}

	renderTemplate(w, r, "account/change_password.html", map[string]interface{}{"form": form})
}
開發者ID:hjqhezgh,項目名稱:gopher,代碼行數:34,代碼來源:account.go

示例12: wrapAuthHandler

// wrapAuthHandler返回符合 go.auth包要求簽名的函數.
func wrapAuthHandler(handler *Handler) func(w http.ResponseWriter, r *http.Request, u auth.User) {
	return func(w http.ResponseWriter, r *http.Request, u auth.User) {
		c := handler.DB.C(USERS)
		user := User{}
		session, _ := store.Get(r, "user")
		c.Find(bson.M{"username": u.Id()}).One(&user)
		//關聯github帳號,直接登錄
		if user.Provider == GITHUB_COM {
			session.Values["username"] = user.Username
			session.Save(r, w)
			http.Redirect(w, r, "/", http.StatusSeeOther)
		}
		form := wtforms.NewForm(wtforms.NewTextField("username", "用戶名", "", wtforms.Required{}),
			wtforms.NewPasswordField("password", "密碼", wtforms.Required{}))
		session.Values[GITHUB_EMAIL] = u.Email()
		session.Values[GITHUB_ID] = u.Id()
		session.Values[GITHUB_LINK] = u.Link()
		session.Values[GITHUB_NAME] = u.Name()
		session.Values[GITHUB_ORG] = u.Org()
		session.Values[GITHUB_PICTURE] = u.Picture()
		session.Values[GITHUB_PROVIDER] = u.Provider()
		session.Save(r, w)

		//關聯已有帳號
		if handler.Request.Method == "POST" {
			if form.Validate(handler.Request) {
				user := User{}
				err := c.Find(bson.M{"username": form.Value("username")}).One(&user)
				if err != nil {
					form.AddError("username", "該用戶不存在")
					handler.renderTemplate("accoun/auth_login.html", BASE, map[string]interface{}{"form": form})
					return
				}

				if !ComparePwd(form.Value("password"), user.Password) {
					form.AddError("password", "密碼和用戶名不匹配")

					handler.renderTemplate("account/auth_login.html", BASE, map[string]interface{}{"form": form, "captchaId": captcha.New()})
					return
				}
				c.UpdateId(user.Id_, bson.M{"$set": bson.M{
					"emal":       session.Values[GITHUB_EMAIL],
					"accountref": session.Values[GITHUB_NAME],
					"username":   session.Values[GITHUB_ID],
					"idref":      session.Values[GITHUB_ID],
					"linkref":    session.Values[GITHUB_LINK],
					"orgref":     session.Values[GITHUB_ORG],
					"pictureref": session.Values[GITHUB_PICTURE],
					"provider":   session.Values[GITHUB_PROVIDER],
				}})
				deleteGithubValues(session)
				session.Values["username"] = u.Name()
				session.Save(r, w)
				http.Redirect(handler.ResponseWriter, handler.Request, "/", http.StatusFound)
			}
		}
		handler.renderTemplate("account/auth_login.html", BASE, map[string]interface{}{"form": form})
	}
}
開發者ID:nosqldb,項目名稱:G,代碼行數:60,代碼來源:account.go

示例13: forgotPasswordHandler

// URL: /forgot_password
// 忘記密碼,輸入用戶名和郵箱,如果匹配,發出郵件
func forgotPasswordHandler(handler *Handler) {
	form := wtforms.NewForm(
		wtforms.NewTextField("username", "用戶名", "", wtforms.Required{}),
		wtforms.NewTextField("email", "電子郵件", "", wtforms.Email{}),
	)

	if handler.Request.Method == "POST" {
		if form.Validate(handler.Request) {
			var user User
			c := handler.DB.C(USERS)
			err := c.Find(bson.M{"username": form.Value("username")}).One(&user)
			if err != nil {
				form.AddError("username", "沒有該用戶")
			} else if user.Email != form.Value("email") {
				form.AddError("username", "用戶名和郵件不匹配")
			} else {
				message2 := `Hi %s,<br>
我們的係統收到一個請求,說你希望通過電子郵件重新設置你在 Golang中國 的密碼。你可以點擊下麵的鏈接開始重設密碼:

<a href="%s/reset/%s">%s/reset/%s</a><br>

如果這個請求不是由你發起的,那沒問題,你不用擔心,你可以安全地忽略這封郵件。

如果你有任何疑問,可以回複<a href="mailto:[email protected]">[email protected]</a>向我提問。`
				code := strings.Replace(uuid.NewUUID().String(), "-", "", -1)
				c.Update(bson.M{"_id": user.Id_}, bson.M{"$set": bson.M{"resetcode": code}})
				message2 = fmt.Sprintf(message2, user.Username, Config.Host, code, Config.Host, code)
				if Config.SendMailPath == "" {
					webhelpers.SendMail(
						"[Golang中國]重設密碼",
						message2,
						Config.FromEmail,
						[]string{user.Email},
						webhelpers.SmtpConfig{
							Username: Config.SmtpUsername,
							Password: Config.SmtpPassword,
							Host:     Config.SmtpHost,
							Addr:     Config.SmtpAddr,
						},
						true,
					)
				} else {
					webhelpers.SendMailExec(
						"[Golang中國]重設密碼",
						message2,
						Config.FromEmail,
						[]string{user.Email},
						Config.SendMailPath,
						true,
					)
				}
				message(handler, "通過電子郵件重設密碼", "一封包含了重設密碼指令的郵件已經發送到你的注冊郵箱,按照郵件中的提示,即可重設你的密碼。", "success")
				return
			}
		}
	}

	handler.renderTemplate("account/forgot_password.html", BASE, map[string]interface{}{"form": form})
}
開發者ID:zhaoshiling1017,項目名稱:gopher,代碼行數:61,代碼來源:account.go

示例14: adminNewNodeHandler

// URL: /admin/node/new
// 新建節點
func adminNewNodeHandler(w http.ResponseWriter, r *http.Request) {
	user, ok := currentUser(r)
	if !ok {
		http.Redirect(w, r, "/signin?next=/node/new", http.StatusFound)
		return
	}

	if !user.IsSuperuser {
		message(w, r, "沒有權限", "你沒有新建節點的權限", "error")
		return
	}

	form := wtforms.NewForm(
		wtforms.NewTextField("id", "ID", "", &wtforms.Required{}),
		wtforms.NewTextField("name", "名稱", "", &wtforms.Required{}),
		wtforms.NewTextArea("description", "描述", "", &wtforms.Required{}),
	)

	if r.Method == "POST" {
		if form.Validate(r) {
			c := db.C("nodes")
			node := Node{}

			err := c.Find(bson.M{"id": form.Value("id")}).One(&node)

			if err == nil {
				form.AddError("id", "該ID已經存在")

				renderTemplate(w, r, "node/new.html", map[string]interface{}{"form": form, "adminNav": ADMIN_NAV})
				return
			}

			err = c.Find(bson.M{"name": form.Value("name")}).One(&node)

			if err == nil {
				form.AddError("name", "該名稱已經存在")

				renderTemplate(w, r, "node/new.html", map[string]interface{}{"form": form, "adminNav": ADMIN_NAV})
				return
			}

			Id_ := bson.NewObjectId()
			err = c.Insert(&Node{
				Id_:         Id_,
				Id:          form.Value("id"),
				Name:        form.Value("name"),
				Description: form.Value("description")})

			if err != nil {
				panic(err)
			}

			http.Redirect(w, r, "/admin/node/new", http.StatusFound)
		}
	}

	renderTemplate(w, r, "node/new.html", map[string]interface{}{"form": form, "adminNav": ADMIN_NAV})
}
開發者ID:RaymondChou,項目名稱:gopher_blog,代碼行數:60,代碼來源:admin.go

示例15: newPackageHandler

// URL: /package/new
// 新建第三方包
func newPackageHandler(handler *Handler) {
	user, _ := currentUser(handler)

	var categories []PackageCategory

	c := handler.DB.C(PACKAGE_CATEGORIES)
	c.Find(nil).All(&categories)

	var choices []wtforms.Choice

	for _, category := range categories {
		choices = append(choices, wtforms.Choice{Value: category.Id_.Hex(), Label: category.Name})
	}

	form := wtforms.NewForm(
		wtforms.NewHiddenField("html", ""),
		wtforms.NewTextField("name", "名稱", "", wtforms.Required{}),
		wtforms.NewSelectField("category_id", "分類", choices, ""),
		wtforms.NewTextField("url", "網址", "", wtforms.Required{}, wtforms.URL{}),
		wtforms.NewTextArea("description", "描述", "", wtforms.Required{}),
	)

	if handler.Request.Method == "POST" && form.Validate(handler.Request) {
		c = handler.DB.C(CONTENTS)
		id := bson.NewObjectId()
		categoryId := bson.ObjectIdHex(form.Value("category_id"))
		html := form.Value("html")
		html = strings.Replace(html, "<pre>", `<pre class="prettyprint linenums">`, -1)
		c.Insert(&Package{
			Content: Content{
				Id_:       id,
				Type:      TypePackage,
				Title:     form.Value("name"),
				Markdown:  form.Value("description"),
				Html:      template.HTML(html),
				CreatedBy: user.Id_,
				CreatedAt: time.Now(),
			},
			Id_:        id,
			CategoryId: categoryId,
			Url:        form.Value("url"),
		})

		c = handler.DB.C(PACKAGE_CATEGORIES)
		// 增加數量
		c.Update(bson.M{"_id": categoryId}, bson.M{"$inc": bson.M{"packagecount": 1}})

		http.Redirect(handler.ResponseWriter, handler.Request, "/p/"+id.Hex(), http.StatusFound)
		return
	}
	handler.renderTemplate("package/form.html", BASE, map[string]interface{}{
		"form":   form,
		"title":  "提交第三方包",
		"action": "/package/new",
		"active": "package",
	})
}
開發者ID:makohill,項目名稱:androidfancier.cn,代碼行數:59,代碼來源:package.go


注:本文中的github.com/jimmykuu/wtforms.NewForm函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。