View不会返回HttpResponse

时间:2018-05-20 20:23:01

标签: django django-views

视图iot.views.post_new未返回HttpResponse对象。它返回了None。 希望大家帮忙。

这是我的views.py文件:

def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST or None)
        if form.is_valid():
            ct = form.save(commit=False)
            ct.author = request.user
            ct.upload_time = request.upload_time
            ct.save()
            return redirect('iot:detail', pk=ct.pk)
    else:
        form = PostForm()
        return render(request, 'iot/post.html', {"form":form})

3 个答案:

答案 0 :(得分:1)

删除else

def post_new(request):
    if request.method == "POST":
        form = PostForm(request.POST or None)
        if form.is_valid():
            ct = form.save(commit=False)
            ct.author = request.user
            ct.upload_time = request.upload_time
            ct.save()
            return redirect('iot:detail', pk=ct.pk)

    form = PostForm()
    return render(request, 'iot/post.html', {"form":form})

答案 1 :(得分:0)

如果表单在您的if子句中无效且您发布了回复,则不会返回render。这就是错误消息所抱怨的内容。

答案 2 :(得分:0)

 The view store.views.updateItem didn't return an HttpResponse object. It returned None instead.


def updateItem(request):
    data = json.loads(request.body)
    productId = data['productId']
    action = data['action']

    print('Action:', action)
    print('Product:', productId)

    customer = request.user.customer
    product = Product.objects.get(id=productId)
    order, created = Order.objects.get_or_create(customer=customer, complete=False)

    orderItem, created = OrderItem.objects.get_or_create(order=order, product=product)

    if action == 'add':
        orderItem.quantity = (orderItem.quantity + 1)
    elif action == 'remove':
        orderItem.quantity = (orderItem.quantity - 1)

    orderItem.save()

    if orderItem.quantity <= 0:
        orderItem.delete()