Aliyasir Naç
Aliyasir Naç

Reputation: 43

django didn't return HttpResponse Object

I saw many answers about this subject of HttpResponse object in Django, but i can't resolve it. Normally, the user enters the information and the database is saved. But this is not happening since I get this error. Is there that can help?

def place_order(request, total=0, quantity=0):
    current_user = request.user

    # If the cart count is less than or equal to 0, then redirect back to shop
    cart_items = CartItem.objects.filter(user=current_user)
    cart_count = cart_items.count()
    if cart_count <= 0:
        return redirect("store")

    grand_total = 0
    tax = 0
    for cart_item in cart_items:
        total += cart_item.product.price * cart_item.quantity
        quantity += cart_item.quantity
    tax = (2 * total) / 100
    grand_total = total + tax

    # ---

    if request.method == "POST":
        form = OrderForm(request.POST)
        if form.is_valid():
            # Store all the billing information inside Order table
            data = Order()
            data.user = current_user
            data.first_name = form.cleaned_data["first_name"]
            data.last_name = form.cleaned_data["last_name"]
            data.phone = form.cleaned_data["phone"]
            data.email = form.cleaned_data["email"]
            data.address_line_1 = form.cleaned_data["address_line_1"]
            data.address_line_2 = form.cleaned_data["address_line_2"]
            data.country = form.cleaned_data["country"]
            data.state = form.cleaned_data["state"]
            data.city = form.cleaned_data["city"]
            data.order_note = form.cleaned_data["order_note"]
            data.order_total = grand_total
            data.tax = tax
            data.ip = request.META.get("REMOTE_ADDR")
            data.save()
            # Generate order number
            yr = int(datetime.date.today().strftime("%Y"))
            dt = int(datetime.date.today().strftime("%d"))
            mt = int(datetime.date.today().strftime("%m"))
            d = datetime.date(yr, mt, dt)
            current_date = d.strftime("%Y%m%d")  # 20210305
            order_number = current_date + str(data.id)
            data.order_number = order_number
            data.save()
            return redirect("store")
    else:
        return redirect("checkout")

Upvotes: 1

Views: 138

Answers (1)

AKX
AKX

Reputation: 168824

There are two branches with three outcomes in your code.

  • if method == POST:
    • YES: if form is valid:
      • YES: do things, return redirect
      • ELSE: do nothing (fall through)
    • ELSE: return redirect to checkout

The problem is the case where you POST the form but it is not valid; in that case you fall through the other else branch and end up returning None, which is naturally not valid.

The simple fix is to unwrap the last else so you always redirect back to checkout if there was no success:

if request.method == "POST":
   if form.is_valid():
       # ...
       return ...
return ...

It would be better and simpler to use a FormView to properly process the data and show form errors to the user.

Upvotes: 2

Related Questions