django 视图products.views.get_product没有返回HttpResponse对象,而是返回了None

bn31dyow  于 2022-11-18  发布在  Go
关注(0)|答案(1)|浏览(160)

/product/apple-ipad-air-5th-gen-64-gb-rom-109-inch-with-wi-fi 5g-purple/上的值错误视图products.views.get_product没有返回HttpResponse对象。它返回了None。
我怎样才能解决这个问题请帮帮我

from django.shortcuts import render,redirect
from products.models import Product
from accounts.models import *
from django.http import HttpResponseRedirect
from products.models import *
from django.utils.timezone import datetime
# Create your views here.

def get_product(request, slug):
    product = Product.objects.get(slug=slug)
    # comment = Comment.objects.get(slug=slug)

    if request.method == "POST":
        star = request.POST.get('star')
        name = request.user.first_name
        body = request.POST.get('body')
        review = Comment(star=star, name=name,body=body,date_added = datetime.today())
        review.product = product
        review.save()
        return redirect(f'/product/{slug}', slug=product.slug)

    try:
        context = {'product': product, }
        if request.GET.get('size'):
            size = request.GET.get('size')
            price = product.get_product_price_by_size(size)
            context['selected_size'] = size
            context['updated_price'] = price
        return render(request, 'product\product.html' , context = context)
    except Exception as e:
        print(e)

`
我正在做一个电子商务网站,我添加审查选项,然后我得到了这个错误

rxztt3cl

rxztt3cl1#

出现此错误是因为return在if语句中。
我对此进行了修改:

try:
        context = {'product': product, }
        if request.GET.get('size'):
            size = request.GET.get('size')
            price = product.get_product_price_by_size(size)
            context['selected_size'] = size
            context['updated_price'] = price
    except Exception as e:
        print(e)
    return render(request, 'product\product.html', context)

相关问题