def impression_edit(request, book_id, impression_id=None): """感想の編集""" # Read a parent object (Book) book = get_object_or_404(Book, pk=book_id) # When modifying impression if impression_id: impression = get_object_or_404(Impression, pk=impression_id) # When adding impression else: impression = Impression() if request.method == 'POST': # Read the posted form data form = ImpressionForm(request.POST, instance=impression) # Validation of form if form.is_valid(): impression = form.save(commit=False) impression.book = book # Set the parent of this impression impression.save() return redirect('cms:impression_list', book_id=book_id) else: # Create a form from Impression instance form = ImpressionForm(instance=impression) return render( request, 'cms/impression_edit.html', dict(form=form, book_id=book_id, impression_id=impression_id))
def impression_edit(request, book_id, impression_id=None): '''感想の編集''' book = get_object_or_404(Book, pk=book_id) # 親の書籍を読む if impression_id: # impression_id が指定されている (修正時) impression = get_object_or_404(Impression, pk=impression_id) else: # impression_id が指定されていない (追加時) impression = Impression() if request.method == 'POST': form = ImpressionForm( request.POST, instance=impression) # POST された request データからフォームを作成 if form.is_valid(): # フォームのバリデーション impression = form.save(commit=False) impression.book = book # この感想の、親の書籍をセット impression.save() return redirect('cms:impression_list', book_id=book_id) else: # GET の時 form = ImpressionForm( instance=impression) # impression インスタンスからフォームを作成 return render_to_response('cms/impression_edit.html', dict(form=form, book_id=book_id, impression_id=impression_id), context_instance=RequestContext(request))
def impression_edit(request, book_id, impression_id=None): book = get_object_or_404(Book, pk=book_id) if impression_id: impression = get_object_or_404(Impression, pk=impression_id) else: impression = Impression() if request.method == 'POST': form = ImpressionForm(request.POST, instance=impression) # save() if form.is_valid(): impression = form.save(commit=False) impression.book = book impression.save() return redirect( 'cms:impression_list', book_id=book_id) # 단순히 페이지 요청만 필요하다. request정보 안가지고 감. #DB반영 작업 else: form = ImpressionForm(instance=impression) return render( request, 'cms/impression_edit.html', dict(form=form, book_id=book_id, impression_id=impression_id))
def impression_edit(request, book_id, impression_id=None): """感想の編集""" book = get_object_or_404(Book, pk=book_id) # 親の書籍を読む if impression_id: # impression_id が指定されているとき、修正する Impression オブジェクトを取得 impression = get_object_or_404(Impression, pk=impression_id) else: # impression_id が指定されていないとき、新しく追加する Impression オブジェクトを作成 impression = Impression() if request.method == 'POST': # POST されたデータからフォームを作成 form = ImpressionForm(request.POST, instance=impression) if form.is_valid(): # フォームのバリデーション # Impression オブジェクトを保存 impression = form.save(commit=False) impression.book = book # この感想の、親の書籍をセット impression.save() # 感想一覧のページにリダイレクト return redirect('cms:impression_list', book_id=book_id) else: # GET のとき、Impression インスタンスからフォームを作成 form = ImpressionForm(instance=impression) # 編集ページを出力 return render(request, 'cms/impression_edit.html', # ビューのテンプレート dict(form=form, book_id=book_id, impression_id=impression_id)) # テンプレートに渡すデータ
def impression_edit(request, book_id, impression_id=None): """感想の編集""" book = get_object_or_404(Book, pk=book_id) # 親の書籍を読む if impression_id: # impression_id が指定されている (修正時) impression = get_object_or_404(Impression, pk=impression_id) else: # impression_id が指定されていない (追加時) impression = Impression() if request.method == 'POST': form = ImpressionForm(request.POST, instance=impression) # POST された request データからフォームを作成 if form.is_valid(): # フォームのバリデーション impression = form.save(commit=False) impression.book = book # この感想の、親の書籍をセット impression.save() return redirect('cms:impression_list', book_id=book_id) else: # GET の時 form = ImpressionForm(instance=impression) # impression インスタンスからフォームを作成 return render(request, 'cms/impression_edit.html', dict(form=form, book_id=book_id, impression_id=impression_id))
def impression_edit(request, book_id, impression_id=None): '''感想の編集''' book = get_object_or_404(Book, pk=book_id) if impression_id: impression = get_object_or_404(Impression, pk=impression_id) else: impression = Impression() if request.method == 'POST': form = ImreppsionForm(request.POST, instance=impression) if form.is_valid(): impression = form.save(commit=False) impression.book = book impression.save() return redirect('cms:impression_list', book_id=book_id) else: form = ImreppsionForm(instance=impression) return render_to_response('cms/impression_edit.html', dict(form=form, book_id=book_id, impression_id=impression_id), context_instance=RequestContext(request))
def impression_edit(request, book_id, impression_id=None): """感想の編集""" book = get_object_or_404(Book, pk=book_id) if impression_id: impression = get_object_or_404(Impression, pk=impression_id) else: impression = Impression() if request.method == 'POST': form = ImpressionForm(request.POST, instance=impression) if form.is_valid(): impression = form.save(commit=False) impression.book = book impression.save() return redirect('cms:impression_list', book_id=book_id) else: form = ImpressionForm(instance=impression) return render(request, 'cms/impression_edit.html', dict(form=form, book_id=book_id, impression_id=impression_id))
def impression_edit(request, book_id, impression_id=None): """感想の編集""" book = get_object_or_404(Book, pk=book_id) if impression_id: impression = get_object_or_404(Impression, pk=impression_id) else: impression = Impression() if request.method == 'POST': form = ImpressionForm(request.POST, instance=impression) if form.is_valid(): impression = form.save(commit=False) impression.book = book impression.save() return redirect('cms:impression_list', book_id=book_id) else: # GET の時 form = ImpressionForm(instance=impression) return render( request, 'cms/impression_edit.html', dict(form=form, book_id=book_id, impression_id=impression_id))
def impression_edit(request, book_id, impression_id=None): """感想の編集""" book = get_object_or_404(Book, pk=book_id) # 親の書籍を読む if impression_id: # impression_idが指定されている(修正時) impression = get_object_or_404(Impression, pk=impression_id) else: # impression_idが指定されていない(追加時) impression = Impression() if request.method == 'POST': form = ImpressionForm( request.POST, instance=impression) # POSTされたrequestデータからフォームを作成 if form.is_valid(): # フォームのバリデーション impression = form.save(commit=False) impression.book = book # この感想の親をセット impression.save() return redirect('cms:impression_list', book_id=book_id) else: # GETの時 form = ImpressionForm(instance=impression) # impressionインスタンスからフォームを作成 return render( request, 'cms/impression_edit.html', dict(form=form, book_id=book_id, impression_id=impression_id))
def impression_edit(request, book_id, impression_id=None): """感想の編集""" book = get_object_or_404(Book, pk=book_id) # 親の書籍を読む if impression_id: #impression_idが指定されている(修正時) impression = get_object_or_404(Impression, pk=impression) else: #impression_idが指定されていない(追加時) impression=Impression() if request.method=="POST": form = ImpressionForm(request.POST, instance=impression) # POSTされたrequestデータからフォームを作成 if form.is_valid(): impression = form.save(commit=False) impression.book=book impression.save() return redirect("cms:impression_list", book_id=book_id) else: #GETの時 form = ImpressionForm(instance=impression) # impression インスタンスからフォームを作成 return render(request, "cms/impression_edit.html", dict(form=form, book_id=book_id, impression_id=impression_id))
def create_impression(self, comment=None): book = Book() book.save() impression = Impression() impression.book = book if comment is not None: impression.comment = comment impression.save()
def impression_edit(request, book_id, impression_id=None): book = get_object_or_404(Book, pk=book_id) if impression_id: # 수정 impression = get_object_or_404(Impression, pk=impression_id) else: # 추가 impression = Impression() if request.method == 'POST': # DB 반영 작업 form = ImpressionForm(request.POST, instance=impression) if form.is_valid(): writer2 = request.session['user_id'] impression.replyname = writer2 impression = form.save(commit=False) impression.book = book impression.save() return redirect('cms:impression_list', book_id=book_id) else: # 화면에 View 작업 form = ImpressionForm(instance=impression) return render( request, "cms/impression_edit.html", dict(form=form, book_id=book_id, impression_id=impression_id))
def test_valid(self): """正常な入力を行えばエラーにならないことを検証""" params = dict(comment='感想') impression = Impression() form = ImpressionForm(params, instance=impression) self.assertTrue(form.is_valid())
def test_either(self): """何も入力しなければエラーになることを検証""" params = dict() impression = Impression() form = ImpressionForm(params, instance=impression) self.assertFalse(form.is_valid())