将表格保存在烧瓶中?

时间:2013-11-08 05:26:04

标签: python flask peewee

我有表单代码,我想通过表单保存数据。 自定义表格是: -

<form class="well span12 c1" name="contacts" method="POST">
<label>First Name</label>
            <input type="text" placeholder="Your First Name" class="span12" required  name="firstname" >
            <label>Last Name</label>
            <input type="text" placeholder="Your Last Name" class="span12" required   name="lastname" >
            <label>Email Address</label>
            <input type="text" placeholder="Your email address" class="span12" required  name="email" >
            <label>Subject</label>
            <select class="span12" name="subject" id="subject" required >
                <option value="na" selected="">Choose One:</option>
                <option value="service">General Customer Service</option>
                <option value="suggestions">Suggestions</option>
                <option value="product">Product Support</option>
            </select>


            <label>Message</label>
            <textarea rows="8" class="input-xlarge span12" id="message" name="message" required ></textarea>

        <button class="btn btn-primary pull-right" style="border-radius:0px;" type="submit">Send</button>

</form>

views.py: -

 @app.route('/contact' ,  methods = ['GET' , 'POST' ])
def contact():
    if request.method == 'POST':
        user = user_email(firstname = request.form['firstname'] , 
            lastname = request.form['lastname'],
            email = request.form['email'],
            subject = request.form['subject'],
            message = request.form['message'])
        user.save()
        return render_template('index.html')
    else:
        return render_template('contact.html')

AttributeError存在错误:“数据库”对象没有属性“模型”。请帮我解决这个问题。

2 个答案:

答案 0 :(得分:2)

您是否已设置数据库并已连接到该数据库?看起来应该是这样的:

# define a database connection
database = peewee.SqliteDatabase('my.db')

class BaseModel(peewee.Model):
    class Meta:
        # this is likely the missing part of your model
        # you need to tell the model which database connection to use.
        database = database

class User(BaseModel):
    first_name = peewee.CharField()
    last_name = peewee.CharField()
    email = peewee.CharField()
    subject = peewee.CharField()
    message = peewee.CharField()

答案 1 :(得分:0)

<form class="well span12 c1" name="contacts" method="POST" action="{{ url_for('contact') }}"> 
  <div class="row-fluid">
        <div class="span6">
            <label>First Name</label>
            <input type="text" placeholder="Your First Name" class="span12" required  name="firstname" >
            <label>Last Name</label>
            <input type="text" placeholder="Your Last Name" class="span12" required   name="lastname" >
            <label>Email Address</label>
            <input type="text" placeholder="Your email address" class="span12" required  name="email" >
            <label>Subject</label>
            <select class="span12" name="subject" id="subject" required >
                <option value="na" selected="">Choose One:</option>
                <option value="service">General Customer Service</option>
                <option value="suggestions">Suggestions</option>
                <option value="product">Product Support</option>
            </select>
        </div>
        <div class="span6">
            <label>Message</label>
            <textarea rows="8" class="input-xlarge span12" id="message" name="message" required ></textarea>
        </div>

        <button class="btn btn-primary pull-right" style="border-radius:0px;" type="submit">Send</button>
    </div>
</form>