def new_topic(request, pk): board = get_object_or_404(Board, pk=pk) # user = User.objects.first() # print (user, "gotten user") # Form implementation crud way # board = get_object_or_404(Board, pk=pk) # if request.method == 'POST': # subject = request.POST['subject'] # message = request.POST['message'] # user = User.objects.first() # topic = Topic.objects.create( # subject=subject, # board=board, # starter=user # ) # post = Post.objects.create( # message=message, # topic=topic, # created_by=user # ) # return redirect('board_topics', pk=board.pk) if request.method == 'POST': form = NewTopicForm(request.POST) if form.is_valid(): topic = form.save(commit=False) topic.board = board topic.starter = request.user topic = form.save() post = Post.objects.create(topic=topic, message=form.cleaned_data['message'], created_by=request.user) return redirect('board_topics', pk=board.pk) else: form = NewTopicForm() context = {'board': board, 'form': form} return render(request, 'new_topic.html', context)
def new_topic(request, pk): """New topic view Renders and returns the new topic view based on the input request :param request: http request :type request: get :param pk: primary key for the boards model :type pk: int :returns: rendered response :rtype: {render} """ board = get_object_or_404(Board, pk=pk) user = User.objects.first() if request.method == 'POST': form = NewTopicForm(request.POST) if form.is_valid(): topic = form.save(commit=False) topic.board = board topic.starter = user topic.save() Post.objects.create(message=form.cleaned_data.get('message'), topic=topic, created_by=user) return redirect('board_topics', pk=board.pk) else: form = NewTopicForm() return render(request, 'new_topic.html', {'board': board, 'form': form})
def new_topic(request, pk): board = get_object_or_404(Board, pk=pk) user_id = request.user.id user = User.objects.get(pk=user_id) if request.method == "POST": form = NewTopicForm(request.POST) if form.is_valid(): topic = form.save(commit=False) topic.board = board topic.starter = user topic.save() post = Post.objects.create( topic=topic, message=form.cleaned_data.get("message"), created_by=user, ) return redirect("boards:topic_posts", pk=board.pk, topic_pk=topic.pk) else: form = NewTopicForm() return render(request, "boards/new_topic.html", { "board": board, "form": form })
def new_topic(request, pk): board = get_object_or_404(Board, pk=pk) user = User.objects.first() # TODO 临时使用一个账号作为登录用户 if request.method == 'POST': form = NewTopicForm(request.POST) if form.is_valid(): topic = form.save(commit=False) topic.board = board topic.starter = user topic.save() post = Post.objects.create( message=form.cleaned_data.get('message'), topic=topic, created_by=user) return redirect('boards:board_topics', pk=board.pk) #TODO 重定向到创建的主题页 else: form = NewTopicForm() return render(request, 'board/new_topic.html', { 'board': board, 'form': form })
def new_topic(request, board_id): try: board = Board.objects.get(pk=board_id) except Board.DoesNotExist: raise Http404 # user = User.objects.first() # TODO: get the currently logged in user if request.method == 'POST': form = NewTopicForm(request.POST) if form.is_valid(): topic = form.save(commit=False) topic.board = board # topic.starter = user topic.starter = request.user topic.save() # topic = form.save() post = Post.objects.create( message=form.cleaned_data.get('message'), topic=topic, created_by=request.user) return redirect('board_topics', board_id=board.pk) else: form = NewTopicForm() # subject = request.POST['subject'] # subject adalah name dari html # message = request.POST['message'] # topic = Topic.objects.create(subject=subject, board=board, starter=user) # post = Post.objects.create(message=message, topic=topic, created_by=user) # return redirect('board_topics', board_id=board.pk) return render(request, 'new_topic.html', {'board': board, 'form': form})
def new_topics(request, pk): board = get_object_or_404(Board, pk=pk) if request.method == "POST": form = NewTopicForm(request.POST) if form.is_valid(): topic = form.save(commit=False) topic.board = board topic.starter = request.user topic.save() Post.objects.create(message=form.cleaned_data.get('message'), topic=topic, created_by=request.user) return redirect('board_topics', pk=board.pk) else: form = NewTopicForm() return render(request, 'topics.html', {'form': form, 'board': board})
def new_topic(request, id): board = get_object_or_404(Board, pk=id) user = User.objects.first() if request.method == 'POST': form = NewTopicForm(request.POST) if form.is_valid(): topic = form.save(commit=False) topic.board = board topic.starter = user topic.save() post = Post.objects.create( message=form.cleaned_data.get('message'), topic=topic, created_by=user) return redirect('topic_posts', pk=id, topic_pk=topic.pk) else: form = NewTopicForm() return render(request, 'new_topic.html', {'board': board, 'form': form})
def topic_new(request, pk): board = get_object_or_404(Board, pk=pk) if request.method == "POST": form = NewTopicForm(request.POST) if form.is_valid(): new_topic = form.save(commit=False) new_topic.starter_id = 1 new_topic.board = board new_topic.save() Post.objects.create(topic=new_topic, message=form.cleaned_data.get("message"), created_by_id=1) return redirect("board_topics", pk=board.pk) else: form = NewTopicForm() return render(request, "boards/topic_new.html", { 'form': form, "board": board })
def new_topic(request, pk): board = get_object_or_404(Board, pk=pk) user = User.objects.first() # TODO: get the currently logged in user if request.method == 'POST': form = NewTopicForm(request.POST) if form.is_valid(): topic = form.save(commit=False) topic.board = board topic.starter = user topic.save() Post.objects.create( message=form.cleaned_data.get('message'), topic=topic, created_by=user ) return redirect('board_topics', pk=board.pk) # TODO: redirect to the created topic page else: form = NewTopicForm() return render(request, 'new_topic.html', {'board': board, 'form': form})
def new_topic(req, pk): board = get_object_or_404(Board, pk=pk) if req.method == 'POST': form = NewTopicForm(req.POST) if form.is_valid(): topic = form.save(commit=False) topic.board = board topic.starter = req.user topic.save() Post.objects.create( message=form.cleaned_data.get('message'), topic=topic, created_by=req.user ) # TODO: to redirect the created topic page return redirect('topic_posts', pk=pk, topic_pk=topic.pk) else: form = NewTopicForm() return render(req, 'new_topic.html', {'board': board, 'form': form})
def new_topic(request, pk): b = get_object_or_404(Board, pk=pk) # TODO: get the currently logged in user if request.method == 'POST': form = NewTopicForm(request.POST) if form.is_valid(): topic = form.save(commit=False) topic.board = b topic.starter = request.user topic.save() post = Post.objects.create( message=form.cleaned_data.get('message'), topic=topic, created_by=request.user) return redirect('topic_posts', pk=pk, topic_pk=topic.pk) else: form = NewTopicForm() return render(request, 'new_topic.html', {'board': b, 'form': form})
def new_topic(request, pk): board = get_object_or_404(Board, pk=pk) # user = User.objects.first() # 从数据库查询出来的第一个用户 if request.method == 'POST': # 发送,提交表格时发送 form = NewTopicForm(request.POST) if form.is_valid(): # 表单有效则返回前面的链接,无效则提示无效不返回 topic = form.save(commit=False) topic.board = board topic.starter = request.user topic.save() post = Post.objects.create( message=form.cleaned_data.get('message'), topic=topic, created_by=request.user) return redirect('board_topics', pk=pk) # TODO:redirect to the created topic page else: pass else: # GET 请求,则新建一个表格 form = NewTopicForm() return render(request, 'new_topic.html', {'board': board, 'form': form})
def new_topic(request, pk): board = get_object_or_404(Board, pk=pk) # 查询有无该主键 if request.method == 'POST': form = NewTopicForm(request.POST) # 对表单进行验证 if form.is_valid(): # 验证成功,将数据保存至数据库 topic = form.save(commit=False) # 后面还要提交数据 topic.board = board topic.starter = request.user topic.save() # 保存至数据库 Post.objects.create( # 在Post中也创建一条记录 message=form.cleaned_data.get('message'), topic=topic, created_by=request.user) return redirect('topic_posts', pk=pk, topic_pk=topic.pk) # 返回渲染后的页面 else: form = NewTopicForm() # 验证不成功创建空表单 return render(request, 'new_topic.html', { 'board': board, 'form': form }) # 返回空表单页面
def new_topic(request, pk): board = get_object_or_404(Board, pk=pk) #user = User.objects.first() # TODO: get the currently logged in user if request.method == 'POST': form = NewTopicForm(request.POST) if form.is_valid(): topic = form.save(commit=False) topic.board = board topic.starter = request.user #here topic.save() post = Post.objects.create( message=form.cleaned_data.get('message'), topic=topic, created_by=request.user #here ) return redirect('topic_posts', pk=pk, topic_pk=topic.pk) # <- here else: form = NewTopicForm() # form.fields # form.message.help_text # form.message.label return render(request, 'boards/new_topic.html', {'board': board, 'form': form})
def new_topic(request, pk): board = get_object_or_404(Board, pk=pk) # TODO: get the currently logged in user # user = User.objects.first() if request.method == 'POST': form = NewTopicForm(request.POST) if form.is_valid(): topic = form.save(commit=False) topic.board = board topic.starter = request.user topic.save() Post.objects.create(message=form.cleaned_data.get('message'), topic=topic, created_by=request.user) # Redirect to the created topic page # IMPORTANT: using topic.pk because a `topic` is an object (Topic Model Instance) and assessing the pk property of the Topic model instance. return redirect('topic_posts', pk=pk, topic_pk=topic.pk) else: form = NewTopicForm() return render(request, 'new_topic.html', {'board': board, 'form': form})
def new_topic(request, pk): board = get_object_or_404(Board, pk=pk) if request.method == 'POST': # subject = request.POST['subject'] # message = request.POST['message'] # # user = User.objects.first() # TODO: 临时使用一个账号作为登录用户 # # topic = Topic.objects.create( # subject=subject, # board=board, # starter=user # ) # # post = Post.objects.create( # message=message, # topic=topic, # created_by=user # ) # # return redirect('board_topics', pk=board.pk) # TODO:redirect to the created topic page form = NewTopicForm(request.POST) if form.is_valid(): topic = form.save(commit=False) topic.board = board topic.starter = request.user topic.save() Post.objects.create(message=form.cleaned_data.get('message'), topic=topic, created_by=request.user) return redirect('topic_posts', pk=pk, topic_pk=topic.pk) else: form = NewTopicForm() return render(request, 'new_topic.html', {'board': board, 'form': form})