How can I redirect the user to the form page while keeping some data from the previous page
Published: 2023-01-23 17:22:03

I am trying to allow customers to place an order for a specific product on my website, and I want to create a button for each product that contain the product's unique id so that I can associate the order with the correct product in admin dashboard. how can I do this?

this is the code I have :

```

## models.py:
from email.policy import default
from unicodedata import name
from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=10, null=True)
    price = models.DecimalField(max_digits=6, decimal_places=0,null=True)
    description = models.TextField(null=True)
    image = models.ImageField(upload_to='photos',null=True)
    productid = models.CharField(max_length=10, null=True)

    def __str__(self):
        return self.name

class Order(models.Model):
    product = models.OneToOneField(Product , on_delete=models.PROTECT , null=True)
    fullname = models.CharField(max_length=10, null=True)
    phone= models.CharField(max_length=10, null=True)
    address = models.CharField(max_length=10, null=True)
    date = models.DateField(blank=True ,  null=True)



## views.py:
def order(request):
    name = request.POST.get('name')
    phone = request.POST.get('phone')
    address = request.POST.get('address')
    date = request.POST.get('date')
    orderdata = Order(fullname = name , phone = phone , address = address , date = date)
    orderdata.save()
    return render(request,'order.html')

def products(request):
    products = Product.objects.all()
    return render(request, 'products.html', {'products':products})



## order.html:
<section class="content" id="content" name="content">
       <div class="row">
           <div class="col-75">
              <div class="container">
                <form method="post">
                   {% csrf_token %}
                  <div class="row">
                    <div class="col-50">
                      <h3>Enter your details</h3>
                      <label for="name"><i class="fa fa-user" ></i> Full Name</label>
                      <input type="text" id="name" name="name"  placeholder="your name..." >
                      <label for="phone"><i class="fa fa-envelope"></i> Phone</label>
                      <input type="text" id="phone" name="phone" placeholder="0996101213">
                      <label for="adr"><i class="fa fa-institution"></i> Address</label>
                      <input type="text" id="adr" name="address" placeholder="542 W. 15th Street">
                      <label for="date">Date to come</label>
                      <input type="date" id="date" name="date">
                    </div>
                  </div>
                  <input type="submit" value="Place Order" class="btn">
                </form>
             </div>
          </div>
 </section>


## products.html:
                  {% for product in products %}
                    <div class="product-card" id="product-card">
                        <div class="product-img" id="product-img">
                          {% if product.image %}
                            <img src="{{ product.image.url }}" alt="">
                          {% endif %}
                        </div>
                        <div class="product-card-bottom" id="product-card-bottom">
                            <div class="category-title" id="category-title">
                              {{ product.name }}
                            </div>
                            <div class="category-title" id="category-title">
                              {{ product.description}} 
                            </div>
                            <div class="product-title" id="product-title">
                              {{ product.price }} <span>SP</span>
                            </div>
                        </div>
                    </div>
                 {% endfor %}

```