LaVOZs

The World’s Largest Online Community for Developers

'; django - how i can create a field for the post - LavOzs.Com

I have model Order

class Order(models.Model):
      STATUS = (
                ('Pending', 'Pending'),
                ('Out for delivery', 'Out for delivery'),
                ('Delivered', 'Delivered'),
               )
      shop = models.ForeignKey(Shop, models.CASCADE, null=True)
      customer = models.ForeignKey(Customer, models.CASCADE, null=True)
      product = models.ForeignKey(Product, models.CASCADE, null=True)
      quantity = models.CharField(max_length=30, null=True, )
      date_created = models.DateTimeField(auto_now_add=True, null=True)
      status = models.CharField(max_length=200, null=True, choices=STATUS, default='Pending')
      note = models.CharField(max_length=1000, null=True)

I create views.py for order

def CreateOrder(request, shop_id, product_id):
    customer = request.user.customer
    shop = Shop.objects.get(id=shop_id)
    product = Product.objects.get(id=product_id)
    quantity = request.POST.get('quantity')
    note = request.POST.get('note')
    order = Order.objects.create(customer=customer, shop=shop, product=product, note=note,
                             quantity=quantity)
    context = {'form': order}
    return render(request,'CreateOrder.html', context)

I have created HTML page for the shop.products here

 <div class="row">
 {% for i in product %}
 <div class="col-lg-4">
  <br>
  <div class="card shadow-lg" style="width: 325px; height: 600px;">
  <div class="card-body">
      <div class="card-header text-white bg-info mb-3 shadow ">{{i.name}} 
 </div><br>

      <div id="carouselExampleSlidesOnly" class="carousel slide" data- 
 ride="carousel">
 <div class="carousel-inner">
 <div class="carousel-item active">
  <img class="d-block w-100 shadow" src="{{ i.product_photo1.url }}" 
 alt="First slide" style="width: 100px; height:300px; float: left; " />
 </div>
 <div class="carousel-item">
  <img class="d-block w-100 shadow" src="{{ i.product_photo2.url }}" 
 alt="Second slide" style="width: 100px; height:300px;float: left; " />
</div>

  </div>
  </div><br>
      <div class="col-sm text-dark">
          Price   ₹ <del>{{i.Print_price}}</del>  {{i.Selling_price}}
  </div>  <div class="col-sm bg-secondary text-white">
 {{i.Brand}}
  </div>
        <br>
      <p class="text-dark " >  {{i.description}}</p>



      <!-- Button trigger modal -->
     <a type="button" class="btn btn-info" data-toggle="modal" data- 
     target="#exampleModalCenter">
     Create Order
     </a>
     <!-- Modal -->
   <div class="modal fade" id="exampleModalCenter" tabindex="-1" 
  role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true" 
   >
   <div class="modal-dialog modal-dialog-centered" role="document">
   <div class="modal-content">
    <div class="modal-header">
    <h5 class="modal-title" id="exampleModalLongTitle">{{i.name}}</h5>
    <button type="button" class="close" data-dismiss="modal" aria- 
    label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">

<div class="container">
 <div class="row">{{quantity}}

 </div>
</div>

  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-secondary" data- 
  dismiss="modal">Close</button>
       <a class="btn btn-sm btn-info  shadow-lg" href="{% url 'CreateOrder' 
 i.shop.id i.id %}" >Create order</a>

  </div>
   </div>
   </div>
</div>
  </div>
</div>
 </div>
{% endfor %}
</div>
</center>
 {% endblock %}

I get the id of the shop and product href="{% url 'CreateOrder' i.shop.id i.id %}" but when I click the button then the Order is created but the quantity and note is null

how I can create an insert field for the quantity and note on the same page show that I get the order with the quantity and the note

You can use forms.py in your app folder

from django import forms

from .models import Order

class orderForm(forms.ModelForm):
          class Meta:
                model = Order
                fields = '__all__' (you can also add a specific field)

then import in your views.py file

from .forms import orderForm

 def index(request):
      form = orderForm()
         if request.method == "POST":
              form = orderForm(request.POST)
                 if form.is_valid():
                    form.save()
      context = {'form':form}
      return render(request, 'index.html', context)

 please read documentation I hope it's Work 
Related
Can I access constants in settings.py from templates in Django?
How to define two fields “unique” as couple
How can I get the full/absolute URL (with domain) in Django?
How to get the current URL within a Django template?
Creating a dynamic choice field
How can I temporarily disable a foreign key constraint in MySQL?
filter json data from Django model
Upload multiple files with one single input