Home > Mobile >  Not able view the data that I am trying to insert in CRUD
Not able view the data that I am trying to insert in CRUD

Time:07-08

I am doing a CRUD of Products.I am struggling to view the data that I am trying to insert. below is the models,serializer(since I am suppsoed to use serializers instead of forms),functions and htmls

class Products(models.Model):
    categories = models.CharField(max_length=15)
    sub_categories = models.CharField(max_length=15)
    color = models.CharField(max_length=15)
    size = models.CharField(max_length=15)
    # image = models.ImageField(upload_to = 'media/',width_field=None,height_field=None,null=True)
    title = models.CharField(max_length=50)
    price = models.CharField(max_length=10)
    sku_number = models.CharField(max_length=10)
    product_details = models.CharField(max_length=300)
    quantity = models.IntegerField(default=0)
    isactive = models.BooleanField(default=True)
class POLLSerializer(serializers.ModelSerializer):
    class Meta:
        model = Products
        fields = "__all__"

def show(request):
    showall = Products.objects.all()
    print(showall)
    serializer = POLLSerializer(showall,many=True)
    print(serializer.data)
    return render(request,'polls/product_list.html',{"data":serializer.data})
def insert(request):
    if request.POST == "POST":
        print('POST',id)
        insert_clothes = {}
        insert_clothes['categories']=request.POST.get('categories')
        insert_clothes['sub_categories']=request.POST.get('sub_categories')
        insert_clothes['color']=request.POST.get('color')
        insert_clothes['size']=request.POST.get('size')
        # insert_clothes['image']=request.POST.get('image')
        insert_clothes['title']=request.POST.get('title')
        insert_clothes['price']=request.POST.get('price')
        insert_clothes['sku_number']=request.POST.get('sku_number')
        insert_clothes['product_details']=request.POST.get('product_details')
        insert_clothes['quantity']=request.POST.get('quantity')
        form = POLLSerializer(data = insert_clothes)
        if form.is_valid():
            form.save()
            # print('data',form.data)
            print(form.errors)
            messages.success(request,'Record Updated successfully :)!!!!')
            return redirect('polls:show')
        else:
            print(form.errors)
    else:
        print('GET',id)
        insert_clothes = Products.objects.all()
        form = POLLSerializer(data = insert_clothes)
        if form.is_valid():
            print(form.errrors)
        return render(request,'polls/product_insert.html')

below is insert html code


<form method="POST">
            {% csrf_token %}
            <table>
                <thead>
                    <tr>
                        <td>Categories</td>
                        <td>
                            <select  aria-label="Default select example" name="categories">
                                <option selected>Open this select menu</option>
                                <option value="1">9-6 WEAR</option>
                                <option value="2">DESI SWAG</option>
                                <option value="3">FUSION WEAR</option>
                                <option value="">BRIDAL WEAR</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>Sub-Categories</td>
                        <td>
                            <input type="text" name="sub_categories" placeholder="SUB_CATEGORIES">
                        </td>
                    </tr>
                    <tr>
                        <td>Colors</td>
                        <td>
                            <input type="text" name="color" placeholder="Enter Colors">
                        </td>
                    </tr>
                    <tr>
                        <td>Size</td>
                        <td>
                            <select  aria-label="Default select example" name="size">
                                <option selected>Open this select menu</option>
                                <option value="SM">SM</option>
                                <option value="M">M</option>
                                <option value="L">L</option>
                                <option value="XL">XL</option>
                                <option value="XXL">XXL</option>
                                <option value="XXXL">XXXL</option>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>Title</td>
                        <td>
                            <input type="text" name="title" placeholder="Enter title">
                        </td>
                    </tr>
                    <tr>
                        <td>Price</td>
                        <td>
                            <input type="text" placeholder="Enter the price" name="price">
                        </td>
                    </tr>
                    <tr>
                        <td>SKU Number</td>
                        <td>
                            <input type="text" name="sku_number">
                        </td>
                    </tr>
                    <tr>
                        <td>product_details</td>
                        <td>
                            <textarea name="product_details" cols="30" rows="10">

                            </textarea>
                        </td>
                    </tr>
                    <tr>
                        <td>quantity</td>
                        <td><input type="number" name="quantity" placeholder="Enter the quantity"></td>
                    </tr>

                    <tr>
                        <td>
                            <input type="submit" value="Insert" />
                        </td>
                        <td>
                            {% if messages %}
                            {% for mess in messages %}
                            <b style="color:green;">{{mess}}</b>
                            {% endfor %}
                            {% endif %}
                        </td>
                    </tr>
                </thead>
            </table>
            <button ><a href="{% url 'polls:show' %}" style="color: red;">Go To Home</a></button>

        </form>

below is the show html

{% for result in data %}
      <tbody>
        <tr>
          <td><b>{{result.categories}}</b></td>
          <td><b>{{result.sub_categories}}</b></td>
          <td><b>{{result.color}}</b></td>
          <td><b>{{result.size}}</b></td>
          <td><b>{{result.title}}</b></td>
          <td><b>{{result.price}}</b></td>
          <td><b>{{result.sku_number}}</b></td>
          <td><b>{{result.product_details}}</b></td>
          <td><b>{{result.quantity}}</b></td>
          <td style="position: relative;left:50px;">
            <a href="/product_edit/{{result.id}}/">
              <button >
                <i ></i>
              </button>
            </a>
          </td>
          <td>
            <a href="/Delete/{{result.id}}/" onclick="return confirm('Are You Sure you want to delete?')">
              <button >
                <i ></i>
              </button>
            </a>
          </td>
        </tr>
      </tbody>
      {% endfor %}

the namings of functions and names of the input and of models appear to be correct so where am I going wrong?

CodePudding user response:

I see that you are constructing dict for your serializer. I'm pretty sure you should use Product instance intead.

Build Product instance from request data and then pass it to serilaizer

product = Product(
    categories=request.POST.get('categories'),
    ... # etc etc
    )

 form = POLLSerializer(product)

or you can even try:

 form = POLLSerializer(request.data)
  • Related