尝试使用python和Bootstrap表单更新/添加MongoDB中的新项时出现错误500

时间:2015-12-10 18:29:08

标签: python html twitter-bootstrap mongodb server

您好我是新手,我正在尝试使用python(包括Jinja模板)和我的html的Bootstrap表单更新(或插入使用upsert)一个名为“faq”的项目到MongoDB。我一直收到POST 500服务器错误,只是希望能够在MongoDB中成功更新/ upsert文档。我正在使用Mongo 2.6和Python 3.4 - 非常感谢任何帮助!

My Bootstrap form / html如下:

<form role="form" action="" method="POST">
   <div class="form-group">
       <label for="category">Category (select one):</label>
       <select class="form-control" id="category" name="category">
            {% for one in catList %}
            <option>{{ one }}</option>
            {% endfor %}
       </select>
   </div>

   <div class="form-group">
       <label for="question">Question:</label>
       <input type="text" class="form-control" id="question" name="question"/>
   </div>

   <div class="form-group">
       <label for="answer">Answer:</label>
       <textarea class="form-control" rows="5" id="answer" name="answer"></textarea>
   </div>

   <div class="form-group">
       <label for="ordinalNum">Ordinal Number</label>
       <input type="text" class="form-control" id="ordinalNum" name="ordinalNum">
   </div>
   <p>This is used to determine the order in which questions are displayed.</p>
   <br /><br />
   <input type="submit" name="Submit" value="Submit" id="Submit" class="btn btn-primary" />
</form>

我的python如下:

@app.route('/faqtemp', methods=['GET', 'POST'])
@nocache
def faqtemp():
"""Renders the faqtemp page."""
user = g.user

cat = Category.objects.filter(isDeleted=False)

catList = {}
for thing in cat:
    if thing.category not in catList:
        catList[thing.category] = thing.ordinalNum
catList = sorted(catList, key=lambda key: catList[key])

links = []
for name in catList:
    name = name.replace(" ", "")
links.append(name)

stuff = FAQ.objects.filter(isDeleted=False)

if request.method == 'GET':
    return render_template('faqtemp.html',
        title='faqtemp',
        message='practice',
        catList=catList,
        stuff=stuff,
        user = user,
        year = datetime.now().year)

elif request.method == 'POST':
     # read form data and save it
    category = request.form['category']
    question = request.form['question']
    answer = request.form['answer']
    ordinalNum = request.form['ordinalNum']

    aFAQ = FAQ(userUUID=user, category=category, question=question, answer=answer, ordinalNum=ordinalNum)
    aFAQ.save()
    #FAQ.update(category, question, answer, ordinalNum, user)

    return render_template('faqtemp.html',
        title='faqtemp',
        message='practice',
        catList=catList,
        stuff=stuff,
        user = user,
        year = datetime.now().year)

else:
    return("<h2>Invalid request</h2>")

我使用python创建了一个FAQ文档:

from mongoengine import *
import datetime

class FAQ(Document):
    question = StringField(required=True)
    answer = StringField(required=True)
    category = StringField(required=True)
    ordinalNum = IntField(required=True)
    isDeleted = BooleanField(required=True, default=False)
    userUUID = StringField(required=True)
    createTS = DateTimeField(required=True, default=None)

def __init__(self, *args, **kwargs):
    Document.__init__(self, *args, **kwargs)
    if not self.createTS:
        self.createTS = datetime.datetime.utcnow()

# def get(self):

def delete(self):
    self.isDeleted = True
    self.save()

def update(self, category, question, answer, ordinalNum, user):
    self.category = category
    self.question = question
    self.answer = answer
    self.ordinalNum = ordinalNum
    self.isDeleted = False
    self.userUUID = user
    self.createTS = datetime.datetime.utcnow()
    self.save()

0 个答案:

没有答案