Michael Gerdov
Michael Gerdov

Reputation: 21

The view orders.views.order_create didn't return an HttpResponse object. It returned None instead

The view orders.views.order_create didn't return an HttpResponse object. It returned None instead.

I tried everything and still get this error I think that the problem is with the urls but i am not sure. This is the views.py for the order checkout. I get the error when I click the checkout everything else is working and only the checkout is giving an error.

from django.shortcuts import render
from django.template import loader
from .models import OrderItem
from .forms import OrderCreateForm
from cart.cart import Cart

def order_create(request):
    cart = Cart(request)

    if request.method == 'POST':
        form = OrderCreateForm(request.POST)
        if form.is_valid():
            order = form.save()
            for item in cart: 
                OrderItem.objects.create(order=order,product=item['product'],price=item['price'],quantity=item['quantity'])

                cart.clear()
                return render(request, 'orders/created.html', {'order': order})

    else:
        form = OrderCreateForm()
    return render(request, 'orders/create.html', {'cart': cart, 'form':form})

the base urls.py

from django.conf.urls import url, include

from . import views

urlpatterns = [
    url(r'^cart/', include('cart.urls', namespace='cart')),
    url(r'^orders/', include('orders.urls', namespace='orders')),
    url(r'^$', views.index, name='index'),
    url(r'^(?P<product_id>[0-9a-f-]+)/$', views.detail, name="detail"),
]

and the order app urls.py

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^create/$', views.order_create, name='order_create'),
]

the url in localhost should be 127.0.0.1:8000/orders/create and the create.html and created.html are in the templates folder in the market folder. I would kindly provide any other information if needed to help me fix the bug.

Upvotes: 2

Views: 968

Answers (3)

Nad&#232;ge
Nad&#232;ge

Reputation: 931

Your view returns a response only in the case where the method is POST which would correspond to the form submission. You need to return a response when method is GET too, to display the form.

One solution could be:

def order_create(request):
    cart = Cart(request)
    form = OrderCreateForm()

    if request.method == 'POST':
        form = OrderCreateForm(request.POST)
        if form.is_valid():
            order = form.save()
            for item in cart:
                OrderItem.objects.create(
                    order=order,product=item['product'],
                    price=item['price'],
                    quantity=item['quantity'])

            cart.clear()
            return render(request, 'orders/created.html', {'order': order})

    return render(request, 'orders/create.html', {'cart': cart, 'form':form})

where the second return will be hit if method is not POST, or if form is not valid. (I'm not sure what Cart is, so I don't know if it should be sent on a GET response...)

Upvotes: 4

zaidfazil
zaidfazil

Reputation: 9235

Edit your view may be somewhat like this, check your indentations first,

def order_create(request):
    cart = Cart(request)

    if request.method == 'POST':
        form = OrderCreateForm(request.POST)
        if form.is_valid():
            order = form.save()
            for item in cart:
                OrderItem.objects.create(order=order,product=item['product'],price=item['price'],quantity=item['quantity'])

            cart.clear()

    else:
        form = OrderCreateForm()
    return render(request, 'orders/create.html', {'cart': cart, 'form':form})

Upvotes: 0

Manvir Singh
Manvir Singh

Reputation: 64

The error seems to be with improper indentation

def order_create(request):
  cart = Cart(request)
  if request.method == 'POST':

At the moment it's like

 def order_create(request):
      cart = Cart(request)
 if request.method == 'POST':

Upvotes: 1

Related Questions