Почему-то выдает ошибку The view core.views.users.OrderCreate didn't return an HttpResponse object. It returned None instead. Переменная order за областью видимости, если я самом низу попытаюсь вернуть ее. В чем я ошибся?
def OrderCreate(request):
cart = Cart(request)
if request.method == 'POST':
form = OrderCreateForm(request.POST)
if form.is_valid():
if request.user.is_authenticated:
user = request.user
prev_order = Order.objects.get(client=user.userprofile)
order = Order.objects.create()
order.client = user.userprofile
order.phone = user.userprofile.phone
order.email = user.email
order.region = prev_order.region
order.address = prev_order.address
order.save()
print(order.client)
for item in cart:
OrderItem.objects.create(order=order, product=item['product'],
price=item['price'],
quantity=item['quantity'],
shipping_cost_pl=item['shipping_cost_pl'],
shipping_cost_ru=item['shipping_cost_ru']
)
cart.clear()
return render(request, 'core/order_created.html', {'order': order})
else:
password = ''.join([random.choice(string.digits + string.hexdigits) for i in range(0, 8)])
user = User.objects.create_user(username=form.cleaned_data['email'], email=form.cleaned_data['email'], password=password)
user.first_name = form.cleaned_data['name']
user.userprofile.phone = form.cleaned_data['phone']
user.save()
order = Order.objects.create()
order.client = user.userprofile
order.phone = form.cleaned_data['phone']
order.email = form.cleaned_data['email']
order.region = form.cleaned_data['region']
order.address = form.cleaned_data['address']
order.save()
for item in cart:
OrderItem.objects.create(order=order, product=item['product'],
price=item['price'],
quantity=item['quantity'],
shipping_cost_pl=item['shipping_cost_pl'],
shipping_cost_ru=item['shipping_cost_ru']
)
cart.clear()
return render(request, 'core/order_created.html', {'order': order})