示例#1
0
def create(question_id):
    question = Question.query.get_or_404(question_id)
    content = request.form["content"]
    answer = Answer(content=content, create_date=datetime.now())
    question.answer_set.append(answer)
    db.session.commit()
    return redirect(url_for("question.detail", question_id=question_id))
示例#2
0
def create(question_id):
    # form = AnswerForm()
    # question = Question.query.get_or_404(question_id)
    # content = request.form['content']
    #
    # answer = Answer(content=content, create_date=datetime.now())
    # question.answer_set.append(answer)
    # db.session.commit()
    # 윗 방법 아랫방법 똑같
    # answer = Answer(question=question, content=content, create_date=datetime.now())
    # db.session.add(answer)
    # db.session.commit()
    form = AnswerForm()
    question = Question.query.get_or_404(question_id)
    if form.validate_on_submit():
        content = request.form['content']
        answer = Answer(content=content, create_date=datetime.now())
        question.answer_set.append(answer)
        db.session.commit()
        return redirect(url_for('question.detail', question_id=question_id))
    return render_template('question/question_detail.html',
                           question=question,
                           form=form)

    return redirect(url_for('question.detail', question_id=question_id))
示例#3
0
def create(question_id):
    # question 객체 조회
    question = Question.query.get_or_404(question_id)

    # answer form
    form = AnswerForm()
    if form.validate_on_submit():
        # request: 브라우저에서 요청한 데이터를 확인할 수 있는 객체
        # form 형식으로 전달된 데이터 중 항목명이 'content' 인 데이터
        content = request.form['content']

        # 질문에 달릴 답변 객체 생성
        # user 필드 반영 - g.user
        answer = Answer(content=content,
                        create_date=datetime.now(),
                        user=g.user)
        question.answer_set.append(answer)
        db.session.commit()

        # return redirect(url_for('question.detail', question_id=question_id))

        # 답변 생성 후 앵커로 이동하기 위한 redirect 수정
        return redirect('{}#answer_{}'.format(
            url_for('question.detail', question_id=question_id), answer.id))

    return render_template('question/question_detail.html',
                           question=question,
                           form=form)
示例#4
0
def create(question_id):
    form = AnswerForm()
    question = Question.query.get_or_404(question_id)
    if form.validate_on_submit():
        content = request.form['content']
        answer = Answer(content=content, create_date=datetime.now(), user=g.user)
        question.answer_set.append(answer)
        db.session.commit()
        return redirect('{}#answer_{}'.format(url_for('question.detail', question_id=question_id), answer.id))
    return render_template('question/question_detail.html', question=question, form=form)
示例#5
0
def create(question_id):
    form = AnswerForm()
    question = Question.query.get_or_404(question_id)
    if form.validate_on_submit():
        content = request.form["content"]
        answer = Answer(content=content, create_date=datetime.now())
        question.answer_set.append(answer)
        db.session.commit()
        return redirect(url_for("question.detail", question_id=question_id))
    return render_template("question/question_detail.html",
                           question=question,
                           form=form)
示例#6
0
def create(question_id):
    question = Question.query.get_or_404(question_id)
    content = request.form['content']
    answer = Answer(content=content, create_date=datetime.now())
    ## 아래 2개 방법 중 하나##
    question.answer_set.append(answer)
    ## or
    # db.session.add(answer)
    ##  -------------   ##

    db.session.commit()
    return redirect(url_for('question.detail', question_id=question_id))
示例#7
0
def create(question_id):
    form = AnswerForm()
    question = Question.query.get_or_404(question_id)
    if form.validate_on_submit():  # 폼에 오류가 없으면 퀘스쳔 디테일에 넘겨라
        content = request.form['content']
        answer = Answer(content=content, create_date=datetime.now())
        question.answer_set.append(answer)
        db.session.commit()

        return redirect(url_for('question.detail', question_id=question_id))

    return render_template('question/detail.html',
                           question=question,
                           form=form)
示例#8
0
def create(question_id):
    form = AnswerForm()
    question = Question.query.get_or_404(question_id)
    if form.validate_on_submit():
        # html file 의 textArea 의 content 키로 되어있는
        content = request.form['content']
        answer = Answer(content=content, create_date=datetime.now())
        # Question 과 Answer 은 relationship 으로 연결되어 있기때문에 backref 인 answer_set 을 사용할 수 있다.
        # db.session.add(answer) 과 같다
        question.answer_set.append(answer)
        db.session.commit()
        return redirect(url_for('question.detail', question_id=question_id))
    return render_template('question/question_detail.html',
                           question=question,
                           form=form)
示例#9
0
def create(question_id):
    form = AnswerForm()
    question = Question.query.get_or_404(question_id)

    if form.validate_on_submit():
        content = request.form['content']
        #g.user는 auth_views.py의 @bp.before_app_request로 만든 로그인 사용자 정보이다. (p.172)
        answer =Answer(content=content, create_date=datetime.now(), user=g.user)
        question.answer_set.append(answer)
    #답변 저장을 위해 다음 코드를 사용할 수도 있다.
    #answer = Answer(question=question, content=content, create_date=datetime.now())
    #db.session.ad(answer)

        db.session.commit()
        #return redirect(url_for('question.detail', question_id=question_id))
        #(p.233)위의 코드를 앵커 엘리먼트로 이동할 수 있도록 아래와 같이 수정함.
        return redirect('{}#answer_{}'.format(url_for('question.detail', question_id=question_id), answer.id))
    return render_template('question/question_detail.html', question=question, form = form)
示例#10
0
def create(question_id):  #()안에 인자값 필수. 몇번에 대한 답인지를 표시해야. int뒤의 것과 동일.
    #form.py 수정 후
    form = AnswerForm()
    question = Question.query.get_or_404(question_id)

    if form.validate_on_submit():  #form에 오류없으면 아래 코드실행해서 등록시켜라.
        #print(request)

        content = request.form['content']
        #answer=Answer(content=content, create_date=datetime.now())
        #방법1)
        # question.answer_set.append(answer)
        # db.session.commit()
        #방법2)
        answer = Answer(question=question,
                        content=content,
                        create_date=datetime.now())
        db.session.add(answer)
        db.session.commit()

        return redirect(url_for('question.detail', question_id=question_id))
    return render_template('question/detail.html',
                           question=question,
                           form=form)  #form에 문제있으면 다시 질문페이지가 뜨게 해라 .
示例#11
0
from pybo.models import Question, Answer
from datetime import datetime
from pybo import db

# question1 = Question(subject='pybo가 무엇인가요?', content='pybo에 대해서 알고 싶습니다.', create_date=datetime.now())
# question2 = Question(subject='플라스크 모델 질문입니다.', content='id는 자동으로 생성되나요?', create_date=datetime.now())

question1 = Question(subject='질문1',
                     content='질문1 입니다.',
                     create_date=datetime.now())
question2 = Question(subject='질문2',
                     content='질문2 입니다.',
                     create_date=datetime.now())

db.session.add(question1)
db.session.add(question2)
db.session.commit()

answer1 = Answer(question=Question.query.get(1),
                 content='저도 잘 모르겠네요 ㅜㅜ',
                 create_date=datetime.now())
answer2 = Answer(question=Question.query.get(2),
                 content='네 자동으로 생성됩니다.',
                 create_date=datetime.now())

db.session.add(answer1)
db.session.add(answer2)
db.session.commit()

# RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.