当我运行new_topic()时,视图没有返回HttpResponse对象,尽管有一个返回

时间:2018-01-08 09:18:38

标签: python django python-3.x django-views

def new_topic(request):
    """add new topic"""
    if request.method != 'POST':
        form = TopicForm()
    else:           
        form = TopicForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('learning_logs:topics'))

    context = {'form': form}
    return render(request, 'learning_logs/new_topic.html', context)

我得到的错误:

视图learning_logs.views.new_topic未返回HttpResponse对象。它返回了None。

我已经搜索了很多相关的问题并对它们进行了测试,但它们没有用。请你帮我一下,谢谢。

1 个答案:

答案 0 :(得分:0)

def new_topic(request):
    """add new topic"""
    if request.method == 'POST':
        form = TopicForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('learning_logs:topics'))
    else:
        form = TopicForm()
    return render(request, 'learning_logs/new_topic.html', {'form': form})