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)
def detail(question_id): form = AnswerForm() question = Question.query.get_or_404(question_id) if form.validate_on_submit(): content = request.form['content'] # --------------------------------- [edit] ---------------------------------- # 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)) return render_template('question/question_detail.html', question=question, form=form)
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)
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(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)
def detail(question_id): form = AnswerForm() question = Question.query.get_or_404(question_id) # answer paging page = request.args.get('page', type=int, default=1) so = request.args.get('so', type=str, default='recent') if so == 'recommend': sub_query = db.session.query(answer_voter.c.answer_id, func.count('*').label('answer_voter')) \ .group_by(answer_voter.c.answer_id).subquery() answer_list = Answer.query \ .outerjoin(sub_query, Answer.id == sub_query.c.answer_id) \ .order_by(sub_query.c.answer_voter.desc(), Answer.create_date.desc()) elif so == 'popular': sub_query = db.session.query(Comment.answer_id, func.count('*').label('num_comment')) \ .group_by(Comment.answer_id).subquery() answer_list = Answer.query \ .outerjoin(sub_query, Answer.id == sub_query.c.answer_id) \ .order_by(sub_query.c.answer_answer.desc(), Answer.create_date.desc()) else: answer_list = Answer.query.filter(Answer.question_id == question_id).order_by(Answer.create_date.desc()) answer_list = answer_list.paginate(page, per_page=5) return render_template('question/question_detail.html', question=question, answer_list=answer_list, form=form, so=so)
def modify(answer_id): menu_id = request.args.get('menu_id', type=int, default=1) answer = Answer.query.get_or_404(answer_id) if g.user != answer.user: flash('수정권한이 없습니다') return redirect( url_for('question.detail', question_id=answer.question.id)) if request.method == 'POST': form = AnswerForm() if form.validate_on_submit(): form.populate_obj(answer) answer.modify_date = datetime.now() db.session.commit() return redirect('{}#answer_{}'.format( url_for('question.detail', question_id=answer.question.id), answer.id)) else: form = AnswerForm(obj=answer) # 메뉴 리스트 menu_list = Menu.query.order_by(Menu.sort_no.asc()) # 메뉴(선택) menu = Menu.query.get_or_404(menu_id) return render_template('answer/answer_form.html', answer=answer, form=form, menu_list=menu_list, menu=menu)
def modify(answer_id): answer = Answer.query.get_or_404(answer_id) # 사용자 글쓴이 일치 확인 if g.user != answer.user: flash('수정권한이 없습니다') return redirect( url_for('question.detail', question_id=answer.question.id)) # POST 방식 if request.method == "POST": form = AnswerForm() if form.validate_on_submit(): # form에 입력된 데이터를 answer에 적용한다. form.populate_obj(answer) answer.modify_date = datetime.now() # 수정일시 저장 db.session.commit() # return redirect(url_for('question.detail', question_id=answer.question.id)) # 답변 수정 후 앵커로 이동하기 위한 redirect 수정 return redirect('{}#answer_{}'.format( url_for('question.detail', question_id=answer.question.id), answer.id)) else: # AnswerForm(obj=answer): 조회한 데이터를 obj 인자로 전달하여 폼을 생성한다. form = AnswerForm(obj=answer) return render_template('answer/answer_form.html', answer=answer, form=form)
def create(question_id): form = AnswerForm() question = Question.query.get_or_404(question_id) # 입력값이 존재할 경우 if form.validate_on_submit(): content = request.form['content'] # request : form 에서 전달된 객체 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)
def answer_create(request, question_id): """ pybo 답변 등록 """ # pk기본키로 모델 객체 1개 출력 -> 없으면 404 페이지 반환 question = get_object_or_404(Question, pk=question_id) # POST방식일 때 입력값넣은 객체 저장 if request.method == "POST": form = AnswerForm(request.POST) if form.is_valid(): answer = form.save(commit=False) answer.author = request.user # 추가한 속성 author 적용 answer.create_date = timezone.now() answer.question = question answer.save() # 앵커 없을 때 (스크롤 관리 위함) #return redirect('pybo:detail', question_id = question.id) # 앵커로 redirect 구성할때 return redirect('{}#answer_{}'.format( resolve_url('pybo:detail', question_id=question.id), answer.id)) # GET방식이면 입력창 반환 else: form = AnswerForm() context = {'question': question, 'form':form} return render(request, 'pybo/question_detail.html', context)
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에 문제있으면 다시 질문페이지가 뜨게 해라 .
def modify(answer_id): answer = Answer.query.get_or_404(answer_id) if g.user != answer.user: flash('수정권한이 없습니다') return redirect(url_for('question.detail', question_id=answer.question.id)) if request.method == "POST": form = AnswerForm() if form.validate_on_submit(): form.populate_obj(answer) answer.updated_at = datetime.now() # 수정일시 저장 db.session.commit() return redirect('{}#answer_{}'.format(url_for('question.detail', question_id=answer.question.id), answer.id)) else: form = AnswerForm(obj=answer) return render_template('answer/answer_form.html', answer=answer, form=form)
def modify(answer_id): answer = Answer.query.get_or_404(answer_id) if g.user != answer.user: flash("수정 권한이 없습니다.") return redirect( url_for("question.detail", question_id=answer.question.id)) if request.method == "POST": form = AnswerForm() if form.validate_on_submit(): form.populate_obj(answer) answer.modify_date = datetime.now() # 수정일시 저장 db.session.commit() return redirect( url_for("question.detail", question_id=answer.question.id)) else: form = AnswerForm(obj=answer) return render_template("answer/answer_form.html", answer=answer, form=form)
def answer_create(request, question_id): question = get_object_or_404(Question, pk=question_id) if request.method == "POST": form = AnswerForm(request.POST) if form.is_valid(): answer = form.save(commit=False) answer.author = request.user answer.create_date = timezone.now() answer.question = question answer.save() return redirect('pybo:detail', question_id=question.id) else: form = AnswerForm() context = {'question': question, 'form': form} return render(request, 'pybo/question_detail.html', context)
def modify(answer_id): answer = Answer.query.get_or_404(answer_id) if g.user != answer.user: flash('수정 권한이 없습니다.') return redirect( url_for('question.detail', question_id=answer.question.id)) if request.method == 'POST': form = AnswerForm() if form.validate_on_submit(): form.populate_obj(answer) answer.modify_date = datetime.now() db.session.commit() return redirect( url_for('question.detail', question_id=answer.question.id)) else: form = AnswerForm(obj=answer) return redirect('{}.#answer_{}'.format( url_for('question.detail', quesiton_id=answer.question.id), answer.id))
def detail(question_id): page = request.args.get('page', type=int, default=1) so = request.args.get('so', type=str, default='recent') form = AnswerForm() # 질문 조회 question = Question.query.get_or_404(question_id) # 조회 수 증가 question.view_cnt = question.view_cnt + 1 db.session.commit() # 답변 조회 if so == 'recommend': sub_query = db.session.query(answer_voter.c.answer_id, func.count('*').label('num_voter')) \ .group_by(answer_voter.c.answer_id).subquery() answer_list = Answer.query \ .filter_by(question=question) \ .outerjoin(sub_query, Answer.id == sub_query.c.answer_id) \ .order_by(sub_query.c.num_voter.desc(), Answer.create_date.desc()) else: # recent answer_list = Answer.query \ .filter_by(question=question) \ .order_by(Answer.create_date.desc()) answer_list = answer_list.paginate(page, per_page=5) # 메뉴 리스트 menu_list = Menu.query.order_by(Menu.sort_no.asc()) # 메뉴(선택) menu = Menu.query.get_or_404(question.menu_id) return render_template('question/question_detail.html', question=question, answer_list=answer_list, menu_list=menu_list, menu=menu, page=page, form=form, so=so)
def answer_modify(request, answer_id): answer = get_object_or_404(Answer, pk=answer_id) if request.user != answer.author: messages.error(request, '수정 권한이 없습니다.') return redirect('pybo:detail', question_id=answer.question.id) if request.method == "POST": form = AnswerForm(request.POST, instance=answer) if form.is_valid(): answer = form.save(commit=False) answer.author = request.user answer.modify_date = timezone.now() answer.save() return redirect('pybo:detail', question_id=answer.question.id) else: form = AnswerForm(instance=answer) context = {'answer': answer, 'form': form} return render(request, 'pybo/answer_form.html', context)
def answer_create(request, question_id): """ pybo Answer """ question = get_object_or_404(Question, pk=question_id) # -- [edit] -- # if request.method == "POST": form = AnswerForm(request.POST) if form.is_valid(): answer = form.save(commit=False) answer.author = request.user #logged in user answer.create_date = timezone.now() answer.question = question answer.save() return redirect('{}#answer_{}'.format( resolve_url('pybo:detail', question_id=question.id), answer.id)) else: form = AnswerForm() context = {'question': question, 'form': form} return render(request, 'pybo/question_detail.html', context)
def detail(question_id): form = AnswerForm() question = Question.query.get_or_404(question_id) return render_template("question/question_detail.html", question=question, form=form)