首页 > 解决方案 > 我如何为帖子创建一个字段

问题描述

我有模型订单

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)

我为订单创建views.py

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)

我在这里为 shop.products 创建了 HTML 页面

 <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 %}

我得到了商店和产品的 ID, href="{% url 'CreateOrder' i.shop.id i.id %}"但是当我点击按钮时,订单被创建,但数量和注释为空

如何在同一页面上为数量和注释创建一个插入字段表明我得到了带有数量和注释的订单

标签: djangodjango-rest-frameworkdjango-viewsdjango-templatesdjango-haystack

解决方案


您可以在 app 文件夹中使用 forms.py

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)

然后导入你的views.py文件

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 

推荐阅读