Example #1
0
def ask_question():
    form = QuestionForm(request.form)
    if request.method == 'POST' and form.validate() and g.user:
        title = form.title.data
        body = form.body.data
        tags = form.tags.data
        if tags == '':
            tags = []
        else:
            tags = [tag.strip().replace(' ', '-') for tag in tags.split(',')]
        new_question = Question(title = title, body = body, 
                    author = g.user,
                    tags = tags)
        new_question.save()
        return redirect('/questions/%s/' % new_question.id)
    elif not g.user:
        flash("Please login")
    elif request.method == 'POST':
        flash("Put it some data please")

    return render_template('ask.html', form = form, title = 'Ask a question')