示例#1
0
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
    })
示例#2
0
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
    })
示例#3
0
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})
示例#4
0
def new_topic(request, pk):
    board = get_object_or_404(Board, pk=pk)
    if request.method == "POST":
        form = NewTopicForm(request.POST, request.FILES or None)
        print("\n\n\nPOST\n\n\n")
        if form.is_valid():
            # print('\n\n\n', request.FILES.getlist('images'), '\n\n\n')
            # print('\n\n\n', form.cleaned_data, '\n\n\n')
            # return HttpResponse("QQ")
            topic = Topic.objects.create(subject=form.cleaned_data['subject'],
                                         starter=request.user,
                                         board=board)
            post = Post.objects.create(
                message=form.cleaned_data.get('message'),
                topic=topic,
                created_by=request.user)
            for image in request.FILES.getlist('images'):
                Photo.objects.create(post=post, title=str(image), file=image)
            Log.objects.create(topic=topic.subject, kind=0, user=request.user)
            messages.success(request,
                             'Your topic was created successfully!',
                             extra_tags='alert')
            return redirect('topic_posts', pk=pk, topic_pk=topic.pk)
    else:
        form = NewTopicForm()
        print(request.method)
    return render(request, '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})
示例#6
0
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)
示例#7
0
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})
示例#8
0
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})
示例#9
0
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
    })
示例#10
0
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})
示例#11
0
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})
示例#12
0
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})
示例#13
0
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
    })  # 返回空表单页面
示例#15
0
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})
示例#16
0
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})
示例#17
0
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})
 def test_invalid_form(self):
     topic = Topic.objects.create(
         subject='foo',
         board=self.board,
         starter=self.user
     )
     post = Post.objects.create(
         message='',
         topic=topic,
         created_by=self.user
     )
     data = {
         'subject': topic.subject,
         'message': post.message,
     }
     form = NewTopicForm(data=data)
     self.assertFalse(form.is_valid())
示例#19
0
 def get_context_data(self, **kwargs):
     kwargs['board'] = self.board
     kwargs['form'] = NewTopicForm()
     return super().get_context_data(**kwargs)