0

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

<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>

视图.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 错误:“数据库”对象没有属性“模型”。请帮我解决这个问题。

4

2 回答 2

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()
于 2013-11-08T07:18:44.660 回答
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>
于 2013-11-11T04:02:42.003 回答