本文整理汇总了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,
})
}
示例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",
})
}
示例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,
})
}
示例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})
}
示例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})
}
示例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,
})
}
示例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})
}
示例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})
}
示例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",
})
}
示例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})
}
示例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})
}
示例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})
}
}
示例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})
}
示例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})
}
示例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",
})
}