'forms.ContactForm对象'没有属性'hidden_​​tag' [英] 'forms.ContactForm object' has no attribute 'hidden_tag'

查看:607
本文介绍了'forms.ContactForm对象'没有属性'hidden_​​tag'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用烧瓶创建联系表单,但在页面呈现时不断收到此错误。

 'forms.ContactForm object'has no attribute'hidden_​​tag'

以下是我的档案:

contact.html

  {%extendslayout.html%} 

{%block content%}
< h2> ;联系与LT; / H2>
< form action ={{url_for('contact')}}method = post>
{{form.hidden_​​tag()}}

{{form.name.label}}
{{form.name}}

{ {form.email.label}}
{{form.email}}

{{form.subject.label}}
{{form.subject}}

{{form.message.label}}
{{form.message}}

{{form.submit}}
< / form>
{%endblock%}

forms.py p>

  from flask.ext.wtf从wtforms导入表格
导入Form,TextField,TextAreaField,SubmitField,校验器

class ContactForm(Form):
name = TextField(Name,[validators.Required()])
email = TextField(Email,[validators.Required() .email()])
subject = TextField(Subject,[validators.Required()])
message = TextAreaField(Message,[validators.Required()])
submit = SubmitField(Send)

routes.py

  from flask import Flask,render_template,从表单请求
import ContactForm

app = Flask(__ name__ )
app.secret_key ='开发键'

@ app.route('/')
def home():
return render_template('home.html ')

@ app.route('/ about')
def about():
return render_template('about.html ')

@ app.route('/ contact',methods = ['GET','POST'])
def contact():$ b $ form = ContactForm()

if request.method =='POST':
return'Form posted。'

elif request.method =='GET':
返回render_template('contact.html',form = form)

if __name__ =='__main__':
app.run(debug = True)

所有其他页面模板都可以正常工作。任何建议都会很棒!感谢您的帮助!

解决方案

您看到的错误是告诉您 forms.ContactForm 没有名为hidden_​​tag的方法。您在 contact.html 的第6行中引用该方法,如下所示:

  {{form.hidden_​​tag()}} 

根据烧瓶文档,这个是实现 CSRF保护



我将从删除引用form.hidden_​​tag()的行开始,然后查看您的表单是否有效。然后返回并根据文档中的这些说明实施CSRF保护。


I am trying to create a contact form using flask but keep getting this error when the page is rendered.

'forms.ContactForm object' has no attribute 'hidden_tag'

Here are my files:

contact.html

{% extends "layout.html" %}

{% block content %}
  <h2>Contact</h2>
  <form action="{{ url_for('contact') }}" method=post>
    {{ form.hidden_tag() }}

    {{ form.name.label }}
    {{ form.name }}

    {{ form.email.label }}
    {{ form.email }}

    {{ form.subject.label }}
    {{ form.subject }}

    {{ form.message.label }}
    {{ form.message }}

    {{ form.submit }}
  </form>
{% endblock %} 

forms.py

from flask.ext.wtf import Form
from wtforms import Form, TextField, TextAreaField, SubmitField, validators

class ContactForm(Form):
  name = TextField("Name", [validators.Required()])
  email = TextField("Email",[validators.Required(), validators.email()])
  subject = TextField("Subject", [validators.Required()])
  message = TextAreaField("Message", [validators.Required()])
  submit = SubmitField("Send")

routes.py

from flask import Flask, render_template, request
from forms import ContactForm

app = Flask(__name__)
app.secret_key = 'development key'

@app.route('/')
def home():
  return render_template('home.html')

@app.route('/about')
def about():
  return render_template('about.html')

@app.route('/contact', methods=['GET', 'POST'])
def contact():
  form = ContactForm()

if request.method == 'POST':
  return 'Form posted.'

elif request.method == 'GET':
  return render_template('contact.html', form=form)

if __name__ == '__main__':
  app.run(debug=True)

All the other page templates are working perfectly fine. Any advice would be awesome! Thanks for the help!

解决方案

The error that you're seeing is telling you that forms.ContactForm has no method called "hidden_tag". You're referencing that method on the 6th line of contact.html like this:

{{ form.hidden_tag() }}

According to the flask documentation, this is the correct way to implement CSRF protection.

I would start by removing the line that references "form.hidden_tag()", then see if your form works. Then go back and implement CSRF protection per those instructions from the documentation.

这篇关于'forms.ContactForm对象'没有属性'hidden_​​tag'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆