Edit:
My goal is to create a small e-commerce. In my index I have a list of products, one of the attributes is a boolean called in_cart which states if
First of all, your if
statement is not reachable. Because you had a return
before it. When you call return
in a function, the next lines of function those are after the return
will not execute.
So you should change the index
function.
Also, you should send an identifier of the product with your post request. Identifier could be the id or any other unique field in your model.
So your code should be something like this:
def index(request):
if request.method == 'POST': # Request is post and you want to update a product.
try:
product = Product.objects.get(unique_field=request.POST.get("identifier")) # You should chnage `unique_field` with your unique filed name in the model and change `identifier` with the product identifier name in your form.
product.in_cart = True
product.save()
return HttpResponse('', status=200)
except Product.DoesNotExist: # There is no product with that identifier in your database. So a 404 response should return.
return HttpResponse('Product not found', status=404)
except Exception: # Other exceptions happened while you trying saving your model. you can add mor specific exception handeling codes here.
return HttpResponse('Internal Error', status=500)
elif request.method == "GET": # Request is get and you want to render template.
products_list = Product.objects.all()
template = loader.get_template('products/index.html')
context = {'products_list': products_list}
return HttpResponse(template.render(context, request))
return HttpResponse('Method not allowed', status=405) # Request is not POST or GET, So we should not allow it.
I added all information you need in the comments of the code. I think you should spend more time on python and django documents. But if you still have any questions, you can ask in comments.
After question edit
If you don't want to use a readonly field in your form, you should make two changes in your code.
First of all, you should add a url with product_id
parameter in your urls.py
file. Something like this:
url(r'^add_to_cart/(?P<product_id>[0-9]+)$', 'add_to_cart_view', name='add_to_cart')
Then you should add separate your add_to_cart view from index
view. Your views should be like this:
def index(request):
if request.method == "GET":
products_list = Product.objects.all()
template = loader.get_template('products/index.html')
context = {'products_list': products_list}
return HttpResponse(template.render(context, request))
return HttpResponse('Method not allowed', status=405)
def cart(request):
cart_list = Product.objects.filter(in_cart = True)
template_cart = loader.get_template('cart/cart.html')
context = {'cart_list': cart_list}
return HttpResponse(template_cart.render(context, request))
def add_to_cart(request, product_id):
if request.method == 'POST':
try:
product = Product.objects.get(pk=product_id)
product.in_cart = True
product.save()
return HttpResponse('', status=200)
except Product.DoesNotExist:
return HttpResponse('Product not found', status=404)
except Exception:
return HttpResponse('Internal Error', status=500)
return HttpResponse('Method not allowed', status=405)
Now you should change the action link of your form to this:
{% url 'add_to_cart' product_id=product.id %}