Place an order for a product in django

I have some products on my website And I want customer to place an order when clicking on a specific product ... and then the order form contains charfield or something from that product model to identify the order for which product is. How can I do this Thanks 😘☺️

I did the products model and I displayed them And I did the order model and I could get the data from user But I stil can't make a correct order

To allow customers to place an order for a specific product on your website, you will need to create a link or button on the product page that will redirect the customer to the order form. The link or button should contain the product's unique identifier, such as its ID or a slug, so that you can associate the order with the correct product.

Here's an example of how you could accomplish this using Django views and templates:

Create a new view for the order form that accepts a product identifier as an argument.

def place_order(request, product_id):
    product = get_object_or_404(Product, id=product_id)
    if request.method == 'POST':
        form = OrderForm(request.POST)
        if form.is_valid():
            order =
            order.product = product
            return redirect('order_success')
        form = OrderForm()
    return render(request, 'place_order.html', {'form': form, 'product': product})

In your product template, add a link or button that will redirect the customer to the order form, passing the product's identifier as an argument.

<a href="{% url 'place_order' %}">Place Order</a>

In your OrderForm you can add hidden input field with the product identifier to be able to access it in the view and associate the order with the correct product

<input type="hidden" name="product_id" value="{{ }}">

In your view you can access the product_id by doing :

product_id = request.POST.get('product_id')
product = Product.objects.get(id=product_id)

This is just an example, and you may need to modify it to fit your specific needs. Hope it helps as a guidance. Cheers!

Back to Top